将 crypto-browserify 添加到 Gatsby 项目

Add crypto-browserify to Gatsby project

我想将 use-shopping-cart (https://useshoppingcart.com/) 添加到我的 Gatsby 项目中。 当我尝试使用它时出现此错误:

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?

中的插件

谢谢!

这种问题(BREAKING CHANGE: webpack < 5 used to include polyfills for node.js...)依赖于webpack has removed polyfills in their new v5 version这一事实,这是use-shopping-cart.

所需要的依赖。

应该通过安装 crypto-browserify(通过 npm i crypto-browserify)并在您的 gatsby-node.jsonCreateWebpackConfig [=27 中将以下后备添加到 webpack 的覆盖配置来修复它=] 应该有效:

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

或者,如果您不想包含 polyfill,您可以使用这样的空模块:

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