Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

webpack warns that process.env.NODE_ENV is defined twice with different values

ErrorWARNING in DefinePlugin

Short answer

Webpack defines process.env.NODE_ENV for you from the mode option, through DefinePlugin. The warning appears when something else defines the same key with a different value: your own DefinePlugin entry, an EnvironmentPlugin, or an explicit optimization.nodeEnv. Keep one source of the value, or turn the built-in one off.

  • Applies to webpack 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Search the config for every place that touches NODE_ENV, then note which mode the build runs in. The warning needs two definitions, so you are looking for the second one.

    grep -n -E "NODE_ENV|nodeEnv|EnvironmentPlugin|DefinePlugin|mode" webpack.config.*
    npx webpack --mode production
  2. 2Match what you see, then jump to its fix

    1. 1The config has a DefinePlugin with 'process.env.NODE_ENV' set to a fixed string, for example 'development', while the build runs with mode production (or the reverse).Your own DefinePlugin entry disagrees with modeFix
    2. 2The config has new webpack.EnvironmentPlugin(['NODE_ENV']) or an object with NODE_ENV, and the shell value differs from mode.EnvironmentPlugin also defines NODE_ENVFix
    3. 3You deliberately set a custom value for process.env.NODE_ENV and the warning still appears.You want your own value, but the built-in one is still onFix

What does the error look like?

  • WARNING in DefinePlugin
  • Conflicting values for 'process.env.NODE_ENV'

Cause 1 of 3

Your own DefinePlugin entry disagrees with mode

1Check

The mode option makes webpack set process.env.NODE_ENV through DefinePlugin: development gives 'development', production gives 'production'. If your config adds the same key with another value, the two clash.

grep -n "process.env.NODE_ENV" webpack.config.*

2Fix

Delete your entry and let mode decide. If you need the value to come from the shell instead, pass it as the mode: webpack --mode production.

// webpack.config.js
module.exports = {
  mode: 'production',
  // no DefinePlugin entry for process.env.NODE_ENV
};

3Verify

Run the build again; the warning is gone and the bundle still contains the value you expect.

npx webpack --mode production

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

EnvironmentPlugin also defines NODE_ENV

1Check

EnvironmentPlugin is a shorthand for DefinePlugin on process.env keys, so listing NODE_ENV in it defines the same key a second time.

grep -n "EnvironmentPlugin" -A3 webpack.config.*

2Fix

Remove NODE_ENV from the EnvironmentPlugin list and keep the other variables there.

new webpack.EnvironmentPlugin({
  DEBUG: false,
})

3Verify

Rebuild and confirm the warning no longer shows.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

You want your own value, but the built-in one is still on

1Check

optimization.nodeEnv defaults to the mode's value and uses DefinePlugin unless it is false. With mode none it already defaults to false.

grep -n "nodeEnv" webpack.config.*

2Fix

Turn the built-in definition off, so only your own entry defines the key.

module.exports = {
  mode: 'production',
  optimization: { nodeEnv: false },
  plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('staging'),
    }),
  ],
};

3Verify

Build and search the output for the value you set.

npx webpack
grep -c "staging" dist/main.js

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Set mode and let webpack define NODE_ENV; do not repeat the key in a plugin.
  • Use the form 'process.env.NODE_ENV' with JSON.stringify for single keys, not a whole process object, which overwrites the process object.
  • Keep the config in one file per environment, or one function that reads argv.mode, so the value cannot be set in two places.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.