Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Error: Can't resolve 'util' in webpack

Tags:

webpack

when i run the script it show me this errors Here is error:

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: { "util": require.resolve("util/") }', install 'util'; If you don't want to include a polyfill, you can use an empty module like this:resolve.fallback: { "util": false } @ ./node_modules/webpack/lib/index.js 74:30-50 77:9-29 @ ./src/googlesheets.js 21:16-34 @ ./src/index.js 1:0-44 2:0-10

like image 717
Gouthamsa Avatar asked Oct 17 '20 13:10

Gouthamsa


3 Answers

The main problem is with Webpack 5. It doesn't preload the polyfill of Node.js. I see that this issue can help you. https://github.com/webpack/webpack/issues/11282

To solve it: npm install util, and add it into webpack.config.js:

module.exports = {
  // ...
  resolve: {
      fallback: {
        util: require.resolve("util/")
      }
  }
  // ...
};
like image 126
Th3Cod3 Avatar answered Nov 09 '22 12:11

Th3Cod3


If your code is multi-target (node & browser), follow the other answers. If your code will only run on nodejs, this suffices:

    module.exports = {
     target: "node", // Or "async-node"
     mode: "production" /* or "development", or "none" */
     /* ... */
    }
like image 20
Alexandre Pereira Nunes Avatar answered Nov 09 '22 10:11

Alexandre Pereira Nunes


For me, it was enough to do yarn add util (alternatively, npm install util). I didn't have to add it to any other files.

like image 12
Elliptica Avatar answered Nov 09 '22 11:11

Elliptica