NODE_ENV
works like any other environment variable (e.g. PATH) and it depends on your platform how to set it:
- Linux and OSX use the simple format
NODE_ENV=production
- Windows uses the
SET NODE_ENV=production
You can explicitly set it before starting your npm run
script by prepending the previous format:
package.json
{
"scripts": {
"build": "NODE_ENV=production webpack"
}
}
cross-env
makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform.
Just set it like you would if it's running on a POSIX system, and cross-env will take care of setting it properly:
First, run npm i -D cross-env
, next change your npm run
script:
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack"
}
}