Webpack 5 – Uncaught ReferenceError: process is not defined

In webpack 5 automatic node.js polyfills are removed. In the migration docs it is mention that

  • Try to use frontend-compatible modules whenever possible.
  • It’s possible to manually add a polyfill for a node.js core module.
    An error message will give a hint on how to achieve that.
  • Package authors: Use the browser field in package.json to make a
    package frontend-compatible. Provide alternative
    implementations/dependencies for the browser.

See this issue.

Now you can refer this PR and check the libs that were removed and install them.
Next add alias for the lib in your webpack config.

For ex.

resolve: {
    alias: {
       process: "process/browser"
    } 
 }

Update:
This can also be done using ProvidePlugin

package.json

"devDependencies": {
   ...
   "process": "0.11.10",
}

webpack.config.js

module.exports = {
  ...
  plugins: [
      new webpack.ProvidePlugin({
             process: 'process/browser',
      }),
  ],
}

Leave a Comment