Gatsby / Webpack Polyfill 问题

6
我正在运行一个Gatsby3网站,像许多人一样,当我尝试使用某些web3插件时,会遇到关于缺失Polyfills的Webpack 5错误。我知道它们不再自动执行,我必须自己处理。
在尝试了我能找到的所有解决方案之后,这个解决方案似乎应该有效,但对我来说并没有。我猜我做错了什么/漏掉了什么。
这是我添加到Gatsby配置中以处理缺失Polyfills的"加密"和"流"的代码:
exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
    resolve: {
      fallback: {
        crypto: require.resolve('crypto-browserify'),
        stream: require.resolve('stream-browserify'),
      },
    },
  })
}

我已经安装了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 }
  • Crypto出现了与上述相同的错误。

这是你完整的 onCreateWebpackConfig 实例吗?你能分享一下你的 package.json 吗? - Ferran Buireu
1个回答

4

我曾经遇到过完全相同的问题。

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

请确保将配置代码放在gatsby-node.js而不是像我看到你的问题时所做的那样放在gatsby-config.js中。


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