Gatsby / Webpack Polyfill 问题

Kei*_*ith 6 node.js polyfills webpack gatsby web3js

我正在运行一个 Gatsby3 网站,和许多人一样,当我尝试使用某些 web3 插件时,我遇到了有关缺少 Polyfills 的 Webpack 5 错误。我知道他们不再自动执行这些操作,我必须自己处理它们。

在尝试了我能找到的所有解决方案之后,这个似乎应该可行,但不适合我。我猜我做错了什么/错过了什么。

这是我添加到 Gatsby 配置中的内容,用于处理“crypto”和“stream”缺少的 polyfill:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
    resolve: {
      fallback: {
        crypto: require.resolve('crypto-browserify'),
        stream: require.resolve('stream-browserify'),
      },
    },
  })
}
Run Code Online (Sandbox Code Playgroud)

我确实安装了 crypto-browserify 和stream-browserify。

然而,同样的错误仍然存​​在:

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: { "stream":
require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
Run Code Online (Sandbox Code Playgroud)
  • 与上面的加密错误相同。

小智 4

我有同样的问题。

您的配置代码是正确的,这就是我用来解决我的问题的方法,将其添加gatsby-node.js到根文件夹中,然后安装两个 npm 软件包。

请务必将配置代码放入其中gatsby-node.js,而不是gatsby-config.js像我看到您的问题时所做的那样。