简体   繁体   English

将加密浏览器添加到 Gatsby 项目

[英]Add crypto-browserify to Gatsby project

I want to add use-shopping-cart ( https://useshoppingcart.com/ ) to my Gatsby project.我想将 use-shopping-cart ( https://useshoppingcart.com/ ) 添加到我的 Gatsby 项目中。 When I try to use it I get this error:当我尝试使用它时,我收到此错误:

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 }

How can I add crypto-browserify to gatsby?如何将crypto-browserify添加到 gatsby? as a plugin inside of gatsby-config.js ?作为gatsby-config.js中的插件?

Thanks!谢谢!

This kind of issues ( BREAKING CHANGE: webpack < 5 used to include polyfills for node.js... ) are related to the fact that webpack has removed polyfills in their new v5 version , which is a needed dependency of use-shopping-cart . This kind of issues ( BREAKING CHANGE: webpack < 5 used to include polyfills for node.js... ) are related to the fact that webpack has removed polyfills in their new v5 version , which is a needed dependency of use-shopping-cart .

It should be fixed by installing crypto-browserify (by npm i crypto-browserify ) and adding the following fallback to webpack's overriding configuration, in your gatsby-node.js , onCreateWebpackConfig API should work:应该通过安装crypto-browserify browserify (通过npm i crypto-browserify )并在您的gatsby-node.js中将以下后备添加到 webpack 的覆盖配置中来修复它, onCreateWebpackConfig API 应该可以工作:

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

Alternatively, if you don't want to include a polyfill, you can use an empty module like this:或者,如果您不想包含 polyfill,则可以使用如下所示的空模块:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM