Search code examples
node.jswindowspowershellnodemon

How can I set NODE_ENV=production with nodemon on Windows?


"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.


Solution

  • 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"
      }