29

UPDATE: As it is explained in the question, this is not a duplicate because I have already tried adding the set keyword before the environment variable and that did not solve the problem.


I am in the process of learning node and typing examples from a book. The first examples deal with showing how the "http" module works and how to create a server to listen to requests. At some point the book asks to add the following line to the scripts section of the package.json file:

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

When I try to run the example with npm run server I get the following error message:

'SERVERPORT' is not recognized as an internal or external command

I haven't been able to find any answer on the internet, at most I found that I could try:

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

But that doesn't help either, the only difference is that instead of the error message I get the command prompt again so apparently the server is never run.

I believe the author used a Linux machine, I am using a Windows 10 laptop.

I am really committed to learn Node and my line of work is on Windows environments. I believe that setting environment variables on package.json is important so I could really use some help in figuring this out.

Thank you.

Sergio Romero
  • 6,477
  • 11
  • 41
  • 71
  • Maybe you should separate the commands with the && sign. `set SERVERPORT=3002 && node ./fiboserver ` – Ibrahim Nov 25 '16 at 14:08
  • Possible duplicate of [How can i set NODE\_ENV=production in Windows?](http://stackoverflow.com/questions/9249830/how-can-i-set-node-env-production-in-windows) – Simon Nov 25 '16 at 14:09
  • @Ibrahim - Thank you. That worked. Could you add an answer so I can set it as the correct solution? – Sergio Romero Nov 25 '16 at 14:14
  • @Simon - Hi. I don't think this is a duplicate from the one you are proposing since the answer for that was only to use "set" before the variable and when I tried that it did not work. – Sergio Romero Nov 25 '16 at 14:16
  • @SergioRomero Hi, I disagree. If you look at the comments, the answer is given. – Simon Nov 25 '16 at 14:24

4 Answers4

56

Make it cross-platform by using cross-env:

"server": "cross-env SERVERPORT=3002 node ./fiboserver"
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Mark Woon
  • 2,046
  • 1
  • 20
  • 26
24

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"

Ibrahim
  • 2,034
  • 15
  • 22
5

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

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
3

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"

ZiiMakc
  • 31,187
  • 24
  • 65
  • 105