The default value of the NODE_ENV variable is set to development by convention. This means that when a Node application is run, it can check the value of the environment variable and do different things based on the value. It is important to note that if nothing has been set for the environment variable, it will be undefined.
To set the environment variable globally to some value, we can run the code from the command line.
For Linux and Mac Operating System:
export NODE_ENV = production
For Windows Operating System:
$env:NODE_ENV = 'production'
We can also apply the environment variable in the application initialization command using the code below assuming we are running a script called app.js.
NODE_ENV = production node app.js
In Windows, the code is different. We use the code below:
set NODE_ENV=production&&node app.js
Because different operating systems require different commands, there is a package available called cross-env which makes the command cross-platform. If we are using express, we can provide environment-specific settings that are automatically called based on the environment we are working on.
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
I use it like this in my npm scripts.
In Node.js, the environment variable NODE_ENV is set to development by default. But it can also be set to development by writing a single line of code.