webpack warns that process.env.NODE_ENV is defined twice with different values
WARNING in DefinePluginShort 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.
Fix it fast
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 production2Match what you see, then jump to its fix
- 1
The 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
The 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
You 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
- 1
What does the error look like?
WARNING in DefinePluginConflicting 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 productionConfirmed by webpack.js.org
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 webpackConfirmed by webpack.js.org
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.jsConfirmed by webpack.js.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix