Setting NODE ENV production
suggest changeProduction deployments will vary in many ways, but a standard convention when deploying in production is to define an environment variable called NODE_ENV and set its value to “production”.
Runtime flags
Any code running in your application (including external modules) can check the value of NODE_ENV:
if(process.env.NODE_ENV === 'production') {
// We are running in production mode
} else {
// We are running in development mode
}
Dependencies
When NODE_ENV is production, npm install skips devDependencies. The same switch on current npm is --omit=dev. --production is the older spelling.
npm install --omit=dev
For setting NODE_ENV you can use any of these methods
method 1: set NODE_ENV for all node apps
cmd.exe:
set NODE_ENV=production
PowerShell:
$env:NODE_ENV = 'production'
Linux or other unix based system :
export NODE_ENV=production
This sets NODE_ENV for current bash session thus any apps started after this statement will have NODE_ENV set to production.
method 2: set NODE_ENV for the current app
Unix shells:
NODE_ENV=production node app.js
cmd.exe:
set NODE_ENV=production&& node app.js
PowerShell:
$env:NODE_ENV = 'production'; node app.js
This sets NODE_ENV for that one process.
method 3: create .env file and use it
Create a .env file and load it when you start the process. env-cmd does that without a shell script. Current env-cmd takes -f for the file:
env-cmd -f .env node app.js
method 4: Use cross-env package
This package allows environment variables to be set in one way for every platform.
After installing it with npm, you can just add it to your deployment script in package.json as follows:
"build:deploy": "cross-env NODE_ENV=production webpack"