Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
Alex McMillan Avatar asked Feb 08 '23 05:02

Alex McMillan


2 Answers

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.

like image 101
Alex McMillan Avatar answered Feb 11 '23 01:02

Alex McMillan


Make it cross-platform by using cross-env:

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

like image 25
Mark Woon Avatar answered Feb 11 '23 01:02

Mark Woon