Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"process is not defined" error after react eject

After running yarn eject in my react project,I get the 'process' is not defined error when calling registerServiceWorker() in index.js. files in scripts and config directories are not modified.

I get the same error for module variable.

like image 670
user16948 Avatar asked Feb 25 '26 17:02

user16948


1 Answers

This happens because React uses process.env to determine whether you're in a development or production environment. From getting-started.md:

Note: by default, React will be in development mode, which is slower, and not advised for production. To use React in production mode, set the environment variable NODE_ENV to production (using envify or webpack's DefinePlugin).

For example:

new webpack.DefinePlugin({
  "process.env": {
    NODE_ENV: JSON.stringify("production")
  }
});

So to build it with Rollup, you need to replace process.env.NODE_ENV with either "development" or "production" – you can use rollup-plugin-replace for this:

rollup({
  entry: 'main.js',
  plugins: [
    replace({
      'process.env.NODE_ENV': JSON.stringify( 'production' )
    })
  ]
}).then(...)
like image 122
Praveen Rao Chavan.G Avatar answered Feb 27 '26 06:02

Praveen Rao Chavan.G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!