I want to make production build of my app using webpack:
"scripts": {
"build": "webpack --mode production",
},
In my webpack.config.js I have this line what I am using in whole config:
const isDevelopment = process.env.NODE_ENV !== 'production';
Usage example:
{
test: /\.s(a|c)ss$/,
exclude: /\.module.(s(a|c)ss)$/,
loader: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
}
but process.env.NODE_ENV
is always undefined
.
I am on Windows 10
Am I doing something wrong?
NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).
env file. It was returning undefined because it did not live inside the server file. It was an oversight.
In Node. js, process. env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process.
Way I found is to use argv.mode
variable inside export like so:
module.exports = (env, argv) => {
const isDevelopment = argv.mode !== 'production';
return {
// Config...
}
}
You could also manually set it like this "build": "set NODE_ENV=production webpack --mode production"
(windows only)
or use cross-env
Run scripts that set and use environment variables across platforms
https://www.npmjs.com/package/cross-env
"build": "cross-env NODE_ENV=production
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With