node.jsreactjswebpackmoralis

i tried to Polyfill modules in webpack 5 but not working (Reactjs)


Hi guys am a newbie in React when i start my project i get the Wepback V5 Error Message

Resolve updated : https://github.com/facebook/create-react-app/issues/11756#issuecomment-1001162736

This What am using!

Os: Win11
Node : v16
React:v17
React-script : v5
Webpack:v5

This Error Message Contains

Crypto
Http
Https
Stream

Error Output

Compiled with problems:X

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

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\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-eth-accounts/lib/index.js 31:74-91

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\node_modules\web3-eth-accounts\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-eth-accounts/node_modules/eth-lib/lib/bytes.js 7:193-227

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\PC\Desktop\Portfolio\portfolio_app\node_modules\web3-eth-accounts\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 }

Image Contain Output

Webpack5 Error Message


Solution

  • I resolve these errors but my app did not render. If you are interested to clear these errors you can paste code right into your-project/node_modules/react-scripts/config/webpack.config.js but these changes can be overwritten after rebuilding your app. Find in module.exports object resolve and write fallback,in your case it's "crypto": require.resolve("crypto-browserify").

    And install dependency npm install crypto-browserify.

    resolve: {
    //   fallback: {
    //     "fs": false,
    //     "tls": false,
    //     "net": false,
    //     "http": require.resolve("stream-http"),
    //     "https": false,
    //     "zlib": require.resolve("browserify-zlib") ,
    //     "path": require.resolve("path-browserify"),
    //     "stream": require.resolve("stream-browserify"),
    //     "util": require.resolve("util/"),
           "crypto": require.resolve("crypto-browserify")
    } 
    

    Or you can add fallback using react-app-rewired as was described in Github https://github.com/facebook/create-react-app/issues/11756 Install react-app-rewired, create config-overrides.js file in the root of your project. My code in the file

    module.exports = function override (config, env) {
        console.log('override')
        let loaders = config.resolve
        loaders.fallback = {
            "fs": false,
            "tls": false,
            "net": false,
            "http": require.resolve("stream-http"),
            "https": false,
            "zlib": require.resolve("browserify-zlib") ,
            "path": require.resolve("path-browserify"),
            "stream": require.resolve("stream-browserify"),
            "util": require.resolve("util/"),
            "crypto": require.resolve("crypto-browserify")
        }
        
        return config
    }
    

    In package.json change scripts from 'start': 'react-scripts start' to 'start': 'react-app-rewired start'. Then start project npm run start or yarn start