webpack : Uncaught ReferenceError: require is not defined

Maybe it will be helpful for someone. Add into call nodeExternals option importType with value 'umd'.

nodeExternals({
   importType: 'umd'
})

You should not use

externals: [nodeExternals()],

in web app. According to https://github.com/liady/webpack-node-externals it is only for backend. Since you use nodeExternals in web app you get CommonJS modules, that expects built in node require function. So just remove it to fix error.


My issue was in my package.json, I had to remove

"type":"module"

It was trying to load the file as an es6 module

https://nodejs.org/api/packages.html#packages_type