【问题标题】:Add crypto-browserify to Gatsby project将加密浏览器添加到 Gatsby 项目
【发布时间】:2021-07-23 18:54:27
【问题描述】:

我想将 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内的插件?

谢谢!

【问题讨论】:

    标签: javascript reactjs webpack stripe-payments gatsby


    【解决方案1】:

    此类问题 (BREAKING CHANGE: webpack &lt; 5 used to include polyfills for node.js...) 依赖于 webpack has removed polyfills in their new v5 version,这是 use-shopping-cart 所需的依赖项。

    应该通过安装crypto-browserifynpm 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,但它确实有效!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-01
    • 2012-06-18
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多