Webpack-cli: --optimization-node-env has no effect

Created on 28 Oct 2020  路  8Comments  路  Source: webpack/webpack-cli

I'm trying to have 2 commands to build my project, one for dev and one for prod

In my webpack.config.js, I'm using process.env.NODE_ENV to add some options related to the dev server if in dev mode:

const app = {...baseConf};
if (process.env.NODE_ENV === 'dev') {
  console.log('Development Mode');
  app.mode = 'development';
  app.devtool = 'inline-source-map';
  app.devServer = {
    contentBase: './dist',
    injectClient: false, // https://github.com/webpack/webpack-dev-server/issues/2484#issuecomment-655211893
   };
}
return app;

In my package.json:

"scripts": {
  "build": "webpack --optimization-node-env=production",
  "build:dev": "webpack --optimization-node-env=dev",
  "start": "webpack serve",
},

What is the current behavior?

When I run npm run build:dev, the process.env.NODE_ENV is undefined

Expected behavior

I was expecting to have NODE_ENV set to dev

Bug

Most helpful comment

As per https://github.com/webpack/webpack/blob/68ffb153badc08efc50ddf43d904e2868fc442f3/test/__snapshots__/Cli.test.js.snap#L1513-L1534

--optimization-node-env should do the job. Sounds like a bug, I will look into it in near future.

All 8 comments

I think you should use mode like webpack --mode=development

https://webpack.js.org/configuration/mode/

As per https://github.com/webpack/webpack/blob/68ffb153badc08efc50ddf43d904e2868fc442f3/test/__snapshots__/Cli.test.js.snap#L1513-L1534

--optimization-node-env should do the job. Sounds like a bug, I will look into it in near future.

@GP4cK Can you provide a reproducible test repo?

@snitin315 here's a repo to reproduce this https://github.com/GP4cK/webpack-node-env

--optimization-node-env=dev is not set process.env.NODE_ENV in the config file, this value only for bundled code

--mode=development never was related to NODE_ENV, because it is bad idea

--optimization-node-env doesn't set process.env.NODE_ENV in webpack.config.js, it sets process.env.NODE_ENV to provided value in source code and it works fine

You can use --env flags to pass custom values to the own config fle

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luillyfe picture luillyfe  路  4Comments

billyjanitsch picture billyjanitsch  路  3Comments

snitin315 picture snitin315  路  5Comments

larkintuckerllc picture larkintuckerllc  路  5Comments

sam-s4s picture sam-s4s  路  5Comments