Setting environment variables in package.json scripts under Windows

Ad

You can set environment variables in Windows with the "SET" command:

set NODE_ENV=production

And you can specify short scripts in a package.json file:

"scripts": {
    "buildDev": "set NODE_ENV=development && webpack",
    "buildProd": "set NODE_ENV=production && webpack",
}

These work perfectly except for one thing: the value of NODE_ENV when webpack begins executing my config file is "development " - note the trailing space.

This prevents my config file from detecting the correct environment (via process.env.NODE_ENV) and returning the appropriate configuration.

Ad

Answer

Ad

Make it cross-platform by using cross-env:

"buildDev": "cross-env NODE_ENV=development webpack"

Ad
source: stackoverflow.com
Ad