192

webpack 5 no longer do auto-polyfilling for node core modules. How to fix it please? PS: I'm a beginner in development so solution must be well described fo me.

errors

Derek Pollard
  • 6,630
  • 6
  • 37
  • 54
Saber
  • 1,969
  • 2
  • 5
  • 8
  • I tried the solutions mentioned here and without success: https://github.com/webpack/webpack/issues/11868 – Saber Oct 30 '20 at 07:30

25 Answers25

84

I was also getting these error's when upgrading from webpack v4 to v5. Resolved by making the following changes to webpack.config.js

added resolve.fallback property

removed node property

{
resolve: {
  modules: [...],
  fallback: {
    "fs": false,
    "tls": false,
    "net": false,
    "path": false,
    "zlib": false,
    "http": false,
    "https": false,
    "stream": false,
    "crypto": false,
    "crypto-browserify": require.resolve('crypto-browserify'), //if you want to use this module also don't forget npm i crypto-browserify 
  } 
},
entry: [...],
output: {...},
module: {
  rules: [...]
},
plugins: [...],
optimization: {
  minimizer: [...],
},
// node: {
//   fs: 'empty',
//   net: 'empty',
//   tls: 'empty'
// },
}

upgrade from v4 to v5 => https://webpack.js.org/migrate/5/#clean-up-configuration

notacorn
  • 2,693
  • 3
  • 17
  • 50
Maqsood Ahmed
  • 1,089
  • 7
  • 15
  • 4
    What do you put in the node_modules : [...] ? – Justin Meskan Feb 19 '21 at 00:34
  • 1
    @JustinMeskan I used it to search node modules or custom modules from root directories. modules: [ path.resolve('./src'), path.resolve('./node_modules'), ], Ref: https://webpack.js.org/configuration/resolve/#resolvemodules – Maqsood Ahmed Feb 19 '21 at 09:05
  • 6
    In what file have you made these edits? Not even the webpack migration guide you linked seems to mention this. – Ruik Sep 09 '21 at 08:38
  • 1
    @Ruik usually we set webpack configs in `webpack.config.js` file in the root folder of your app. https://webpack.js.org/configuration/ – Maqsood Ahmed Sep 10 '21 at 10:30
  • 1
    where do we add resolve.fallback property??? – Brian Ivander T. P. Jan 01 '22 at 15:31
  • @BrianIvanderT.P. you should add it inside `webpack.config.js` file. – Maqsood Ahmed Jan 01 '22 at 15:36
  • @MaqsoodAhmed I do not appear to have this file (webpack.config.js) in the root folder of my app??? – johnDoe Feb 12 '22 at 10:06
  • 1
    @johnDoe I think you created project using `create-react-app`. So, you can may get config file by ejecting project OR by going into `react-scripts` node_modules folder. https://stackoverflow.com/questions/48395804/where-is-create-react-app-webpack-config-and-files – Maqsood Ahmed Feb 12 '22 at 10:51
  • Thanks so much! I went from 190 errors to five firebase-related errors and one google auth error. This is a lot more manageable. – brohjoe Apr 21 '22 at 14:01
56

I think most the answers here would resolve your issue. However, if you don't need Polyfills for your node development, then I suggest using target: 'node' in your Webpack module configuration. This helped resolve the issue for me.

Here is some documentation on the answer: https://webpack.js.org/concepts/targets/

enter image description here

Tr1gZer0
  • 1,126
  • 9
  • 17
50

Re-add support for Node.js core modules with node-polyfill-webpack-plugin:

With the package installed, add the following to your webpack.config.js:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
    // Other rules...
    plugins: [
        new NodePolyfillPlugin()
    ]
}
Richie Bendall
  • 5,657
  • 3
  • 32
  • 49
  • 5
    This fixed most of the errors, but not all. I still needed to fix 'fs' and 'path-browserify' using @lukas-bach 's solution. – kenecaswell Jan 20 '21 at 22:13
  • 1
    @kenecaswell I'm open to suggestions for improvement. How should `fs` be polyfilled in the browser? Should we just lie with a shim? – Richie Bendall Jan 21 '21 at 01:39
  • 1
    I take back what I said, node-polyfill-webpack-plugin worked great for my client bundling. I was also bundling server side code which needed 'fs' and 'path-browserify'. Once I applied your plugin to only my client build it worked great. – kenecaswell Feb 01 '21 at 23:52
  • 1
    everyone keeps mentioning webpack.config.js file, I cannot find it in the root folder of my app. Please can soneone advise? – johnDoe Feb 12 '22 at 10:03
  • @johnDoe You'll need to find where Webpack is getting its config from – Richie Bendall Feb 12 '22 at 15:49
  • 1
    @johnDoe if you are using create-react-app, the webpack.config.js can be found in node_modules/react-scripts (https://stackoverflow.com/a/48396154/3563737). To edit the webpack configuration of a CRA project, you might want to consider the react-app-rewired package (https://www.npmjs.com/package/react-app-rewired). – Wasbeer Feb 28 '22 at 07:04
22

Looks like you've used the path package which is not included in webpack builds by default. For me, extending the webpack config like this helped:

{
  // rest of the webpack config
  resolve: {
    // ... rest of the resolve config
    fallback: {
      "path": require.resolve("path-browserify")
    }
  },
}

Also install path-browserify via npm install path-browserify --save-dev or yarn add path-browserify --dev if you're using yarn.

Lukas Bach
  • 2,997
  • 1
  • 23
  • 29
  • If it will be shipped to the browser, should it be a dev dependency? – Mustafa Mar 11 '21 at 21:27
  • 3
    @MMansour if you build your app as a webpack project and just ship the built artifacts, it shouldn't matter. Differentiation between dev and normal dependencies is only relevant if you publish your package to NPM, in which case consumers of your package will only install non-dev dependencies. – Lukas Bach Mar 12 '21 at 00:09
22

I faced this issue with create-react-app which by default gave me

"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"

I fixed it by just changing react-scripts to version 4.0.3.

Mazin Luriahk
  • 592
  • 5
  • 6
16

You need React => v17 React scripts=> v5 webpack => v5

To Fix The Problem

1) Install

"fs": "^2.0.0",  // npm i fs
"assert": "^2.0.0",  // npm i assert
"https-browserify": "^1.0.0", // npm i https-browserify
"os": "^0.1.2", // npm i os
"os-browserify": "^0.3.0", // npm i os-browserify
"react-app-rewired": "^2.1.9", //npm i react-app-rewired
"stream-browserify": "^3.0.0", // stream-browserify
"stream-http": "^3.2.0", //stream-http

2) Creating config-overrides.js in root directory Image

3) Add configs to config-overrides.js

const webpack = require('webpack');
module.exports = function override(config, env) {
    config.resolve.fallback = {
        url: require.resolve('url'),
        fs: require.resolve('fs'),
        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'),
        buffer: require.resolve('buffer'),
        stream: require.resolve('stream-browserify'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}

3) Change packages.json

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

Problem solved :)

Letton
  • 169
  • 1
  • 5
16

As per Web3 documentation:

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/];
Vitto
  • 321
  • 2
  • 5
  • 1
    This helped I added the config-overrides file to my root and then called crypto in a js file in src folder. When I did that I got the error: Module not found: Error: You attempted to import /Users/username/abja/identity/node_modules/crypto-browserify/index.js which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. – Mabel Oza Apr 17 '22 at 15:40
  • its work for me – user16102215 Apr 26 '22 at 07:48
13

Create React App just released v5 which now implements Webpack v5. This is going to fully break many libraries which includes web3 as the required Node core library polyfills will be missing.

To resolve this quickly you can change this in your package.json:

"react-scripts": "^5.0.0"

To this

"react-scripts": "4.0.3"

After this:

rm -r node_modules

rm package-lock.json

npm install
secret
  • 151
  • 1
  • 6
  • Unfortunately this solution leaves me with this frustrating circumstance: https://stackoverflow.com/questions/70721056/transparent-iframe-blocks-mouse-event-when-using-react-scripts-start – Gus T Butt Feb 16 '22 at 17:06
  • I've encountered the same scenario as Gus T Butt pointed out, but I'd prefer to degrade the react-scripts to 4.0.3 until this issue is fixed. As @chokshen mentioned in this thread[1] the fixed dependency version to the resolutions section of package.json works well "resolutions": { "//": "See https://github.com/facebook/create-react-app/issues/11773", "react-error-overlay": "6.0.9" }, my environment: Node: 16.14.2 react-scripts: 4.0.3 react: 17.0.2 [1]https://stackoverflow.com/questions/70721056/transparent-iframe-blocks-mouse-event-when-using-react-scripts-start – Kelei Ren May 03 '22 at 12:50
12

It happen with me while I reinstall the node modules my webpack current version is 5.38.1, I have fixed the issue with npm i path-browserify -D after installation you have to update your webpack.config.js resolve{} with fallback: {"fs": false, "path": require.resolve("path-browserify")} while not using "fs": false it show errors i.e: Module not found: Error: Can't resolve 'fs' in '/YOUR DIRECTORY ...' so don't forget to add it; with other stuff it look like:

module.exports = {
   ...
   resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],// other stuff
    fallback: {
      "fs": false,
      "path": require.resolve("path-browserify")
    }
  },
};

remove node property if it exist in your webpack.config.js file

Azhar
  • 642
  • 9
  • 20
4

npm install path-browserify and then try to change webpack configuration to include:

module.exports = {
    ...
    resolve: {
        alias: {
            path: require.resolve("path-browserify")
        }
    }
};
Daksh
  • 976
  • 10
  • 22
4

My environment is like this:

  • React => v17
  • React scripts=> v5
  • webpack => v5

To Fix The Problem follow the below instructions

1- Install below packages

yarn add fs assert https-browserify os os-browserify stream-browserify stream-http react-app-rewired

2- Create config-coverrides.js in Root dir of your project next to the package.json

add the below code to it:

const webpack = require('webpack');
module.exports = function override(config, env) {
    config.resolve.fallback = {
        url: require.resolve('url'),
        fs: require.resolve('fs'),
        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'),
        buffer: require.resolve('buffer'),
        stream: require.resolve('stream-browserify'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}

3- Change the packages.js like the below

  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject"
  },
Hamed Taheri
  • 148
  • 1
  • 11
  • Your clean explanation however, gives a memory issue when in CI/CD of Gitlab, it uses significantly more memory than without the react-app-rewired trick. At the end, I had to downgrade to react-scripts 4.0.3 again – Egbert Nierop Feb 27 '22 at 19:37
4

Method 1

  • Open project/node_modules/react-scripts/config/webpack.config.js

  • In fallback add "crypto": require.resolve("crypto-browserify")

resolve: {
   fallback: {
       "crypto": require.resolve("crypto-browserify")
   }
} 
  • Install npm i crypto-browserify
  • Restart your app.

Above method doesn't work if you commit since we aren't node_modules

Method 2

  • Install patch-package: yarn add patch-package

  • Install the needed pollyfills.(do an initial build of your application and it will tell you.)

  • Modify node_modules/react-scripts/config/webpack.config.js. Here's an example. This is taken from Webpack's docs.

module.exports = {
  //...
  resolve: {
    fallback: {
      assert: require.resolve('assert'),
      buffer: require.resolve('buffer'),
      console: require.resolve('console-browserify'),
      constants: require.resolve('constants-browserify'),
      crypto: require.resolve('crypto-browserify'),
      domain: require.resolve('domain-browser'),
      events: require.resolve('events'),
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      os: require.resolve('os-browserify/browser'),
      path: require.resolve('path-browserify'),
      punycode: require.resolve('punycode'),
      process: require.resolve('process/browser'),
      querystring: require.resolve('querystring-es3'),
      stream: require.resolve('stream-browserify'),
      string_decoder: require.resolve('string_decoder'),
      sys: require.resolve('util'),
      timers: require.resolve('timers-browserify'),
      tty: require.resolve('tty-browserify'),
      url: require.resolve('url'),
      util: require.resolve('util'),
      vm: require.resolve('vm-browserify'),
      zlib: require.resolve('browserify-zlib'),
    },
  },
};
  • Don't add all of them, add only the ones you need.

Make sure you install the packages first before modifying the webpack config.

  • Run yarn patch-package react-scripts. This will generate a patch (this should be committed in your repo going forward).

  • Add a postinstall script to package.json: "postinstall": "yarn patch-package". Now, anytime, someone installs npm deps on this project, will get the patch you created in step 3 applied automatically.

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "postinstall": "yarn patch-package"
  },
Maddu Swaroop
  • 3,277
  • 2
  • 18
  • 32
1

you want to used in

const nodeExternals = require('webpack-node-externals');

add in webpack.config.js

target: "node",
devtool: "source-map",
externals: [nodeExternals()],
Dharman
  • 26,923
  • 21
  • 73
  • 125
  • 3
    please take time to read this: https://stackoverflow.com/help/how-to-answer –  Apr 15 '21 at 13:01
1
npm install assert --save

npm install buffer --save

For anyone facing similar issue, just install the missing modules. These modules are reported as missing because they are part of node.js, but are available separately also via the npm.

Tyler2P
  • 2,182
  • 12
  • 17
  • 28
Adesiyan Tope
  • 29
  • 1
  • 9
1

I'm using create-react-app with craco, and I encountered the following errors when upgrading to webpack 5:

'buffer'


Module not found: Error: Can't resolve 'buffer' in '/Users/geek.neo/propolis-portal/node_modules/safe-buffer'

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

This was resolved simply by installing the buffer package with npm install -D buffer.

'fs'


Module not found: Error: Can't resolve 'fs' in '/Users/geek.neo/propolis-portal/node_modules/line-navigator'

This was resolved by setting a webpack fallback in the craco configuration craco.config.js:

module.exports = {
    style: {
        postcssOptions: {
            plugins: [
            require('tailwindcss'),
            require('autoprefixer'),
            ],
        },
    },
    webpack: {
        configure: (webpackConfig, { env, paths }) => {
            // eslint-disable-next-line no-param-reassign
            webpackConfig.resolve.fallback = {
                fs: false,
            };
            return webpackConfig;
        },
    },
}
therightstuff
  • 598
  • 1
  • 10
  • 16
1

This is happening with the new vue-cli upgrade (v5). In order to fix it (the empty modules way), you have to change vue.config.js this way:

configureWebpack: (config) => {
  config.resolve.fallback = {
    ...config.resolve.fallback,
    // Include here the "empty" modules
    url: false,
    util: false,
    querystring: false,
    https: false,
  };
}
Jesús Fuentes
  • 802
  • 1
  • 10
  • 32
1

My solution for VUE

const { defineConfig } = require('@vue/cli-service')
const webpack = require('webpack');
module.exports = defineConfig({
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
      }),
      new webpack.ProvidePlugin({
          process: 'process/browser',
      })
    ],
    resolve: {
      fallback: {
        "os": require.resolve("os-browserify/browser"),
        "url": require.resolve("url/"),
        "crypto": require.resolve("crypto-browserify"),
        "https": require.resolve("https-browserify"),
        "http": require.resolve("stream-http"),
        "assert": require.resolve("assert/"),
        "stream": require.resolve("stream-browserify"),
        "buffer": require.resolve("buffer")
      }
    }
  },

  transpileDependencies: [
    'vuetify'
  ]

})
Javva
  • 26
  • 3
0

My app threw the same error yesterday. I spent hours reading questions/answers here on SO and trying some. What has worked for me is this:

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

thisLinda
  • 55
  • 1
  • 8
0

If it's Twilio you use:

The Twilio module is not intended for use in client-side JavaScript, which is why it fails for your Angular application. Twilio uses your Account SID and your Auth Token, the use in the client side represents a risk; So the best is to use it on the server side and to use the API.

Baba
  • 133
  • 1
  • 6
0

With create-react-app v17 and scripts 5.0.0 you need to add a fallback to your webpack.config.js and install 'process'.

   `module.exports = function (webpackEnv) {
      .........
      return {
       .........
      resolve: {
      ..........
      fallback: { "process": require.resolve("process/browser") },`
Noud
  • 121
  • 7
0

Secret's answer very nearly worked for me (I don't have enough reputation yet to comment on there, sorry!)

After having followed the steps in their answer it then told me that because of the difference in required versions of eslint, I should add SKIP_PREFLIGHT_CHECK=true to a .env file in the project, so I just added it to my existing one.

It would then successfully build (finally!) But then I noticed, in Chrome at least, that I couldn't click on anything or even select any text. Turns out that there's still an Iframe over the top of everything that can be removed in Inspector. - This applies when running a dev build, npm run start, I am not sure if it does it on a production build.

I must say this sudden change IMO really hasn't been very well thought through!

Gavin
  • 51
  • 1
  • 7
0

my problem was that I was trying to use JSON.parse, then when I started writing , auto complete showed me json(note small letters) though I renamed it to JSON. when I pressed enter it automatically imported (import { json } from "express/lib/response";) it was the cause and I didn't notice it at all , later in development my app was crushing and it took me about six hours to note, since the app is quite big.

so if none of the above solutions work just see if you didn't import something .

-1

ERROR : [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.

Answer: node_modules > react-scripts > config > webpack.config.js

Add in webpack.config.js file:

resolve: {
      fallback: { 
        "http": require.resolve("stream-http") ,
         "path": require.resolve("path-browserify") 
        },
}

Dharman
  • 26,923
  • 21
  • 73
  • 125
RJ45
  • 1
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '22 at 07:37
-1

I like to keep things simple, no need to run npm run-script eject until here, so I downgrade back to react-scripts@4.0.3 instead of 5.0.0 Until now, this is still not fixed in Webpack

Sadly, because that includes a bunch of retired packages.

Egbert Nierop
  • 1,668
  • 12
  • 16
-1

for me, I just removed an unused import called:

import res from "express/lib/response"

and it fixed it!

Raz Asido
  • 14
  • 1