Alex McMillan
Alex McMillan

Reputation: 17952

Setting environment variables in package.json scripts under Windows

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.

Upvotes: 6

Views: 6507

Answers (2)

Mark Woon
Mark Woon

Reputation: 2196

Make it cross-platform by using cross-env:

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

Upvotes: 3

Alex McMillan
Alex McMillan

Reputation: 17952

I managed to fix this by, funnily enough, removing the space:

"buildDev": "set NODE_ENV=development&& webpack"

which (to me at least) seems just wrong. I expected this would have resulted in a syntax error and a NODE_ENV value of development&&, but it works perfectly - albeit being ugly.

Upvotes: 9

Related Questions