node-util: Undeclared use of node's `process` causes exception in browser

Webpack 5 no longer ships polyfills for nodejs builtin modules, and recommends using this module if a module need util, and the module README does say it should work in a browser. However, the module causes a exception when importing the resulting bundle in the browser:

vendor.2cdb7b12f7e46077b4bd.js:92301 Uncaught ReferenceError: process is not defined

This is causes by this code, which expects to be able to use the nodejs process module without importing it:

https://github.com/browserify/node-util/blob/6b828255a7f407efcd7e4d2c54ddb43256e491fb/util.js#L109

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I hit this one today as well. Like @ljharb said, looks like you can use ProvidePlugin like this:

// webpack needs to be explicitly required
const webpack = require('webpack')

module.exports = {

/* ... rest of the config here ... */

  plugins: [
    // fix "process is not defined" error:
    // (do "npm install process" before running the build)
    new webpack.ProvidePlugin({
      process: 'process/browser',
    }),
  ]
}

More here.

I wasn’t 100% in love with using the process package for this project so I ended up using DefinePlugin instead:

plugins: [  
  new webpack.DefinePlugin({
    'process.env.NODE_DEBUG': JSON.stringify(process.env.NODE_DEBUG)
  })
]

Both solutions seem to work.

For those who use Vite or Nuxt3

Error with process.env.NODE_DEBUG

solutions ==>

  1. Install polyfill using npm or yarn
yarn add @esbuild-plugins/node-globals-polyfill
  1. Add to your vite or nuxt config (NUXT example, vite similar)
import {NodeGlobalsPolyfillPlugin} from '@esbuild-plugins/node-globals-polyfill'

export default defineNuxtConfig({
vite: {
        optimizeDeps: {
            esbuildOptions: {
                define: {
                    global: 'globalThis'
                },
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true
                    }),
                ]
            }
        },
    }
})

With this config Web3 and other work perfect

The best fix, unfortunately, is to use ProvidePlugin in your config to replicate the functionality webpack 5 removed.

// webpack needs to be explicitly required
const webpack = require('webpack')

module.exports = {

/* ... rest of the config here ... */

  plugins: [
    // fix "process is not defined" error:
    // (do "npm install process" before running the build)
    new webpack.ProvidePlugin({
      process: 'process/browser',
    }),
  ]
}

@brenc Thanks you man soooo much for clear, referenced reply. This saved me so much time. after 5 days of stress. This worked for me. 2nd one magically didn’t work. Because of other packages.

Solve in !67

Can we get a fix on this soon? Webpack v5 is now the main version.

For the env check, that would be enough, but we also use process.nextTick in the callbackify implementation

For those who use Vite or Nuxt3

Error with process.env.NODE_DEBUG

solutions ==>

  1. Install polyfill using npm or yarn
yarn add @esbuild-plugins/node-globals-polyfill
  1. Add to your vite or nuxt config (NUXT example, vite similar)
import {NodeGlobalsPolyfillPlugin} from '@esbuild-plugins/node-globals-polyfill'

export default defineNuxtConfig({
vite: {
        optimizeDeps: {
            esbuildOptions: {
                define: {
                    global: 'globalThis'
                },
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true
                    }),
                ]
            }
        },
    }
})

With this config Web3 and other work perfect

I’m getting ReferenceError: Buffer is not defined when building app

All node modules may require node core module polyfills; it’s the job of a working node module bundler to handle that. Those that don’t, are broken.