2
"scripts": {
    "start": "SET NODE_ENV=staging && nodemon app",
    "production": "set NODE_ENV=production && nodemon app",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

it's not working ⚒

my staging port is 3000 and my production port is 5000 but no matter what I do nodemon is not taking the value.

3 Answers 3

8

The likely problem is that the space character before && becomes part of the environment-variable values, so that the values are staging and production - note the trailing space - rather than staging and production.

The simplest way to avoid this is to remove the space before && (it looks awkward, but it works):

"scripts": {
    "start": "SET NODE_ENV=staging&& nodemon app",
    "production": "set NODE_ENV=production&& nodemon app",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

To avoid such problems in general, it's best to assign variable values in cmd.exe using the form
set "variable=value"
, i.e. to enclose the name, the =, and the value in "..." overall, which delimits the value explicitly, irrespective of subsequent (unquoted) whitespace; therefore, the following should work too (with the embedded " characters escaped as \", as required by JSON).

"scripts": {
    "start": "SET \"NODE_ENV=staging\" && nodemon app",
    "production": "set \"NODE_ENV=production\" && nodemon app",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
4
  • 1
    "production": "set NODE_ENV=production&& nodemon app" It works ( btw I'm using Powershell ) Commented Jun 16, 2021 at 3:49
  • Glad to hear it, @SabbirHasan. That you're using PowerShell is incidental here, because what matters is the shell that npm uses when scripts are invoked, which is cmd.exe by default, and your script definitions indeed use cmd.exe syntax.
    – mklement0
    Commented Jun 16, 2021 at 3:51
  • should I switch to Linux for big node project? Commented Jun 16, 2021 at 3:55
  • @SabbirHasan, not necessarily, but if you want your package.json files to also work on Unix-like platforms as-is, switching to use of WSL on Windows may help. However, this is a bigger discussion than can fit in a comments exchange here.
    – mklement0
    Commented Jun 16, 2021 at 3:59
1

I know this is a bit late but, environment variables can be set before running app with nodemon like this without using the set name=value method

"scripts": {
    "start": "NODE_ENV=staging nodemon app",
    "production": "NODE_ENV=production nodemon app",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
0

This worked for me, without "set", for example NODE_ENV=development nodemon app.js

1
  • This duplicates what was suggested earlier and has little detail. This response looks better if it was a comment on an existing response. if you had more detail I would have put it though, though as its own answer. Commented Jun 14, 2023 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.