Vortilion
Vortilion

Reputation: 424

Webpack mode production NODE_ENV still undefined

I'm trying to work with webpack 4 and a tutorial. I could set up a project, but it seems the setting for

process.env.NODE_ENV 

if running webpack via "webpack --mode production" is still undefined. I don't understand how to make the loader evaluate the correct mode to extractcss in seperate files or not:

const styles = {
test: /\.(sa|sc|c)ss$/,
use: [
    {loader: process.env.NODE_ENV !== 'production' ? 'style-loader' : miniCssExtractPlugin.loader},
    {loader:'css-loader'},
    {loader:'sass-loader'}
]

};

If I hardcode it to "production" it does work, but I want to be able to select it via webpack --mode! Isn't that possible?

Upvotes: 1

Views: 695

Answers (2)

PlayMa256
PlayMa256

Reputation: 6841

If I hardcode it to "production" it does work, but I want to be able to select it via webpack --mode! Isn't that possible?

Unfortunately, this is one of the complanies of many loaders/plugins authors. NODE_ENV is not set based on the type of mode that you are running. One of the problems here is that it is not possible to update node options/flags after the process has already started.

You'll have to manually set cross-env NODE_ENV=production webpack --mode (cross-env is a env setter, that works on windows and unix).

Upvotes: 4

Vilsad P P
Vilsad P P

Reputation: 1569

you can try with SET NODE_ENV=production (or export instead of SET if you are doing it from linux)

Upvotes: 0

Related Questions