webpack: Error `Uncaught TypeError: util.inherits is not a function`

I want to use console.log in my script, so I added node: { console: true } to my configuration. Now when I try to load the script, it reports Uncaught TypeError: util.inherits is not a function. The error is occuring in the Webpack version of assert.js at the line:

util.inherits(assert.AssertionError, Error);

I nosed around a bit, and it seems that that file is being parsed twice (if I put a log statement at the top of the file, it is written to the console twice). The first time, the util object is correct and has an inherits property that is a function. The second time, util is still an object but it has no properties.

Does anyone have an idea what might be going on here? If not, I will try to put together a simple reproducible use case.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 4
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

This is seriously annoying :hurtrealbad:. I did this in the end, because the node-util probably won’t ever get updated again:

npm i browser-assert

Then in your config, monkeypatch node-libs-browser:

const nodeLibsBrowser = require('node-libs-browser')
nodeLibsBrowser.assert = require.resolve('browser-assert')
nodeLibsBrowser.util = require.resolve('util')
module.exports = {
  resolve: {
    mainFields: ['browser', 'module', 'main']
  }
};

Adding this to the webpack config solved it for me.

i got this error becasue i had an util folder in my repository in which i created an index.ts / index.js which overshadowed the node_module/util module. Solved it by just renaming that index.ts thereby preventing my ./util to pass as a node module.

npm i --save util@0.10.0 works for me temporally

I am also was to run

cd node_modules/node-libs-browser && npm i util@0.10.0