Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables package.json in Windows 10

Tags:

node.js

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.

like image 641
Sergio Romero Avatar asked Nov 25 '16 14:11

Sergio Romero


People also ask

How do I get environment variables in package json?

For a test you can see the env variables by running npm run env-linux or npm run env-windows , and test that they make it into your app by running npm run start-linux or npm run start-windows .


2 Answers

Make it cross-platform by using cross-env:

"server": "cross-env SERVERPORT=3002 node ./fiboserver"
like image 80
Mark Woon Avatar answered Sep 28 '22 02:09

Mark Woon


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"

like image 39
Ibrahim Avatar answered Sep 28 '22 00:09

Ibrahim