简体   繁体   中英

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?

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).

您可以尝试使用SET NODE_ENV = production(如果从Linux进行操作,则可以导出而不是SET)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM