Setting environment variables package.json in Windows 10

On Windows you have to separate the command of setting a variable from the one which runs the server with the && operator. That being said, you have to do something like this:

"server": "set SERVERPORT=3002 && node ./fiboserver"


Make it cross-platform by using cross-env:

"server": "cross-env SERVERPORT=3002 node ./fiboserver"

I've gone through the same problem and used one of the following methods.


Method 1

If I run (without using the npm wrapper script)

HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start

Starting the development server...

it works fine. As Quentin says,

Must be something to do with how npm shells out then

To fix it, I've gone to package.json and changed the "start" script to

"start": "./node_modules/.bin/react-scripts start",

Then npm start works fine.


Method 2

Use the cross-env package.

For that install it using the following command

npm i cross-env

then go to package.json and change it to

"start": "cross-env ./node_modules/.bin/react-scripts start",

And then running npm start will also work fine:

cross-env npm start


You can set bash as package.json scripts runner and it's will work in windows and linux.

Just set it once:

  • for yarn yarn config set script-shell /bin/bash
  • for npm npm config set script-shell /bin/bash

Or "C:\\Program Files\\git\\bin\\bash.exe" instead /bin/bash

It's will allow you to run npm script cross-platform: "server": "SERVERPORT=3002 node ./fiboserver"

Tags:

Node.Js