【问题标题】:Webpack 5 错误 - 无法解决 ReactJS 项目中的“crypto”、“http”和“https”
【发布时间】:2022-01-22 12:47:23
【问题描述】:

背景

我使用 npx create-react-app client 创建了一个新的 React 项目,但遇到了一些使用 Webpack 5 的问题。最初,我在使用 assertosstream 时遇到了错误,但通过安装它们并修复了它们包括他们在webpack.config.js。该文件位于client/src 文件夹中。

这是错误的样子:

Compiled with problems:

    ERROR in ./node_modules/eth-lib/lib/bytes.js 9:193-227

    Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Username\Projects\testProject\client\node_modules\eth-lib\lib'

    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 }

=================================================================

    ERROR in ./node_modules/web3-providers-http/lib/index.js 30:11-26

    Module not found: Error: Can't resolve 'http' in 'C:\Users\Username\Projects\testProject\client\node_modules\web3-providers-http\lib'

    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: { "http": require.resolve("stream-http") }'
        - install 'stream-http'
    If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "http": false }

=================================================================

    ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 39:12-28

    Module not found: Error: Can't resolve 'https' in 'C:\Users\Username\Projects\testProject\client\node_modules\xhr2-cookies\dist'

    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: { "https": require.resolve("https-browserify") }'
        - install 'https-browserify'
    If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "https": false }

代码

这就是我的webpack.config.js 文件现在的样子。

module.exports = {
  resolve: {
    fallback: {
      assert: require.resolve('assert'),
      crypto: require.resolve('crypto-browserify'),
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      os: require.resolve('os-browserify/browser'),
      stream: require.resolve('stream-browserify'),
    },
  },
};

下面是我的package.json 文件。

{
  "dependencies": {
    "assert": "^2.0.0",
    "crypto-browserify": "^3.12.0",
    "dotenv": "^10.0.0",
    "https-browserify": "^1.0.0",
    "os": "^0.1.2",
    "stream": "^0.0.2",
    "stream-http": "^3.2.0"
  }
}

从上面可以看出,我已经安装了错误建议的包(crypto-browserifystream-httphttps-browserify),并将它们包含在webpack.config.js 文件中。但是,错误仍然存​​在。

我该如何解决这个问题?谢谢!

【问题讨论】:

    标签: node.js reactjs webpack web3 polyfills


    【解决方案1】:

    对于包括 web3 在内的许多包来说,这看起来像是一个新问题,因为它们与 Webpack v5 不兼容,而不为 polyfils 添加回退。

    此处指出的问题:https://github.com/facebook/create-react-app/issues/11756

    我通过将后备添加到我的 webpack.config.js 文件中解决了这个问题;

    module.exports = {
        resolve: {
            fallback: {
                assert: require.resolve('assert'),
                crypto: require.resolve('crypto-browserify'),
                http: require.resolve('stream-http'),
                https: require.resolve('https-browserify'),
                os: require.resolve('os-browserify/browser'),
                stream: require.resolve('stream-browserify'),
            },
        },
    };
    

    使用;

    "dependencies": {
        "@testing-library/jest-dom": "^5.16.1",
        "@testing-library/react": "^12.1.2",
        "@testing-library/user-event": "^13.5.0",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-scripts": "5.0.0",
        "web-vitals": "^2.1.2",
        "web3": "^1.6.1"
      },
    

    但也需要但在构建过程中出现编译错误:

    FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory" error
    

    这已通过添加到我的 .env 文件中解决;

    GENERATE_SOURCEMAP=false
    

    【讨论】:

    • 请不要对多个问题粘贴相同的答案。如果问题可以用相同的答案来回答,请对其中一个问题发布一个好的答案,并将其他问题标记为重复。如果它们不是重复的,您应该针对特定问题定制一个答案,而不仅仅是剪切/粘贴。
    【解决方案2】:

    我发现的替代解决方案是降级到webpack@4.44.2react-scripts@4.0.3,而不是使用Webpack 5。在找到更好的解决方案之前,我将暂时使用这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多