将 crypto-browserify 添加到 Gatsby 项目中

8

我想在我的Gatsby项目中添加use-shopping-cart (https://useshoppingcart.com/)。 当我尝试使用它时,我会遇到这个错误:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules 
by default.
This is no longer the case. Verify if you need this module and configure a
polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "crypto":
require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }

我该如何将 crypto-browserify 添加到gatsby中?作为gatsby-config.js内的插件?

谢谢!

1个回答

14

这种问题 (BREAKING CHANGE: webpack < 5 used to include polyfills for node.js...) 的解决取决于 webpack 在新的v5版本中移除了polyfills,而这是use-shopping-cart所需的依赖。

通过安装 crypto-browserify (通过npm i crypto-browserify) 并在你的 gatsby-node.js 中添加以下回退来覆盖webpack的配置,可以解决此问题,使用onCreateWebpackConfig API应该会起作用:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
   resolve: {
      fallback: {
        crypto: require.resolve('crypto-browserify'),
      },
    },
  })
}

或者,如果你不想使用polyfill,你可以像这样使用一个空模块:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
   resolve: {
      fallback: {
        "crypto": false
      },
    },
  })
}

谢谢,我只需要在上面的解决方案中使用 crypto 而不是 path,但它起作用了!非常感谢! - Sanne Wintrén

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接