admin管理员组

文章数量:1289880

I am facing issue with React application while pilation. Please find the issue below and screenshot.

ERROR in ./node_modules/web3-providers-http/lib/index.js 30:11-26
Module not found: Error: Can't resolve 'http' in '/Users/rohit/Downloads/Personal/web3/react-minting-website/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 }
 @ ./node_modules/web3-core-requestmanager/lib/index.js 56:16-46
 @ ./node_modules/web3-core/lib/index.js 23:23-58
 @ ./node_modules/web3/lib/index.js 32:11-31
 @ ./src/index.js 10:0-24 14:13-17

On scrutiny, I found out Issue is with web3 related dependencies :


/@web3-react/core
/@web3-react/injected-connector

Can someone please help me with the same? I am using LTS versions, What are stable versions of these?

I am facing issue with React application while pilation. Please find the issue below and screenshot.

ERROR in ./node_modules/web3-providers-http/lib/index.js 30:11-26
Module not found: Error: Can't resolve 'http' in '/Users/rohit/Downloads/Personal/web3/react-minting-website/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 }
 @ ./node_modules/web3-core-requestmanager/lib/index.js 56:16-46
 @ ./node_modules/web3-core/lib/index.js 23:23-58
 @ ./node_modules/web3/lib/index.js 32:11-31
 @ ./src/index.js 10:0-24 14:13-17

On scrutiny, I found out Issue is with web3 related dependencies :

https://www.npmjs./package/web3
https://www.npmjs./package/@web3-react/core
https://www.npmjs./package/@web3-react/injected-connector

Can someone please help me with the same? I am using LTS versions, What are stable versions of these?

Share Improve this question edited Sep 8, 2022 at 21:05 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Dec 24, 2021 at 12:35 Rohit MittalRohit Mittal 4271 gold badge7 silver badges20 bronze badges 1
  • Does this answer your question? Webpack 5 Errors - Cannot Resolve 'crypto', 'http', and 'https' in ReactJS Project – TylerH Commented Sep 8, 2022 at 21:08
Add a ment  | 

3 Answers 3

Reset to default 7

web3.js has updated their readme to included troubleshooting steps. Ref. link.

Web3 and Create-react-app

If you are using create-react-app version >=5 you may run into issues building. This is because NodeJS polyfills are not included in the latest version of create-react-app.

Solution

  • Install react-app-rewired and the missing modules

If you are using yarn:

yarn add --dev react-app-rewired process crypto-browserify stream-browserify assert stream-http https-browserify os-browserify url buffer

If you are using npm:

npm install --save-dev react-app-rewired crypto-browserify stream-browserify assert stream-http https-browserify os-browserify url buffer process
  • Create config-overrides.js in the root of your project folder with the content:
const webpack = require('webpack');

module.exports = function override(config) {
    const fallback = config.resolve.fallback || {};
    Object.assign(fallback, {
        "crypto": require.resolve("crypto-browserify"),
        "stream": require.resolve("stream-browserify"),
        "assert": require.resolve("assert"),
        "http": require.resolve("stream-http"),
        "https": require.resolve("https-browserify"),
        "os": require.resolve("os-browserify"),
        "url": require.resolve("url")
    })
    config.resolve.fallback = fallback;
    config.plugins = (config.plugins || []).concat([
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer']
        })
    ])
    return config;
}
  • Within package.json change the scripts field for start, build and test. Instead of react-scripts replace it with react-app-rewired

before:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
},

after:

"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
},

The missing Nodejs polyfills should be included now and your app should be functional with web3.

  • If you want to hide the warnings created by the console:

In config-overrides.js within the override function, add:

config.ignoreWarnings = [/Failed to parse source map/];

If you are using create-react-app version >=5 you may run into issues building. This is because NodeJS polyfills are not included in the latest version of create-react-app.


Currently CRA ships react-scripts with version 5.0.0. Instead of ejecting CRA, just downgrade react-scripts to version 4.0.3. I was facing the same issue, downgrading worked for me.

First remove old version

npm uninstall react-scripts

Then run the following:

npm i [email protected]

as webpack grows in size, they removed the polyfills in webpack5. Looks like you are using create-react-app (CRA) and webpack configuration is not exposed to the user in CRA. you can expose it using eject. you might have this script in package.json:

   "eject": "react-scripts eject"

so run npm run eject. This is not remended because it means that you will no longer benefit from the updates of CRA.

you can handle ejecting with either rewire or craco.

After you get the webpack configuration, you need to add resolve property to webpack config and install all those required packages :

resolve: {
    extensions: [".js", ".css"],
    alias: {
      // add as many aliases as you like!
      // optional
      ponents: path.resolve(__dirname, "src/ponents"),
    },
    fallback: {
      // path: require.resolve("path-browserify"),
      fs: false,
      assert: require.resolve("assert/"),
      os: require.resolve("os-browserify/browser"),
      constants: require.resolve("constants-browserify"),
      stream: require.resolve("stream-browserify"),
      crypto: require.resolve("crypto-browserify"),
      http: require.resolve("stream-http"),
      https: require.resolve("https-browserify"),
    },
  },

I have webpac5 Boilerplate. you can use it if you want:

  • Since there are too many polyfills, instead of manually installing all, you can use node-polyfill-webpack-plugin package. instead of fallback property

     const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
    
    plugins: [
      new HtmlWebpackPlugin({
        title: "esBUild",
        template: "src/index.html",
      }),
      // instead of fallback
      new NodePolyfillPlugin(),
    
      new webpack.ProvidePlugin({
        process: "process/browser",
        Buffer: ["buffer", "Buffer"],
        React: "react",
      }),
    ],
    

webpack5 boilerplate github repo

Web3 and Create-react-app If you are using create-react-app version >=5 you may run into issues building. This is because NodeJS polyfills are not included in the latest version of create-react-app.

Refer the Solution in the Below link

https://github./ChainSafe/web3.js#troubleshooting-and-known-issues

本文标签: javascriptWeb3 IssueReact Application not compilingStack Overflow