Esbuild: "Process is not defined" error while building react app

Created on 17 Feb 2020  路  1Comment  路  Source: evanw/esbuild

I've tried to use esbuild. It is impressively fast :)
But i faced issue with env variables.
Once i run compiled code i got following error:
Uncaught ReferenceError: process is not defined
I looked into line were error occurred

   "use strict";
    if (process.env.NODE_ENV === "production") {
      module.exports = require(1172 /* ./cjs/react.production.min.js */);
    } else {
      module.exports = require(1171 /* ./cjs/react.development.js */);
    }

React create app allows to consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_.

Most helpful comment

The way this works with esbuild is to use --define like this:

esbuild src/index.js--outfile=dist/index.js --bundle '--define:process.env.NODE_ENV="production"' 

This replaces all instances of process.env.NODE_ENV with the string "production". I have also included enough dead code elimination logic in esbuild to be able to handle cases like this. The code above will turn into this:

"use strict";
if (true) {
  module.exports = require(1172 /* ./cjs/react.production.min.js */);
} else {
  module.exports = null;
}

The require() call in the false branch turns into null because esbuild knows it's never executed, and should not be included in the bundle. If you also pass --minify that gives esbuild permission to compact the structure of your code, so it will then turn into this:

module.exports = require(1172 /* ./cjs/react.production.min.js */);

The --define feature can replace any identifier or member expression in your code, not just process.env.NODE_ENV.

>All comments

The way this works with esbuild is to use --define like this:

esbuild src/index.js--outfile=dist/index.js --bundle '--define:process.env.NODE_ENV="production"' 

This replaces all instances of process.env.NODE_ENV with the string "production". I have also included enough dead code elimination logic in esbuild to be able to handle cases like this. The code above will turn into this:

"use strict";
if (true) {
  module.exports = require(1172 /* ./cjs/react.production.min.js */);
} else {
  module.exports = null;
}

The require() call in the false branch turns into null because esbuild knows it's never executed, and should not be included in the bundle. If you also pass --minify that gives esbuild permission to compact the structure of your code, so it will then turn into this:

module.exports = require(1172 /* ./cjs/react.production.min.js */);

The --define feature can replace any identifier or member expression in your code, not just process.env.NODE_ENV.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elektronik2k5 picture elektronik2k5  路  3Comments

aelbore picture aelbore  路  3Comments

mohsen1 picture mohsen1  路  3Comments

wcastand picture wcastand  路  4Comments

ayox picture ayox  路  4Comments