19

I am developing in node.js and wanted to take into account both production and development environment. I found out that setting NODE_ENV while running the node.js server does the job. However when I try to set it in package.json script it gives me the error:

NODE_ENV is not recognised as an internal or external command

Below is my package.json

{
  "name": "NODEAPT",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "NODE_ENV=development node ./bin/server",
    "qa2": "NODE_ENV=qa2  node ./bin/server",
    "prod": "NODE_ENV=production node ./bin/server"
  },
  "dependencies": {
    "body-parser": "~1.15.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.13.4",
    "fs": "0.0.1-security",
    "jade": "~1.11.0",
    "morgan": "~1.7.0",
    "oracledb": "^1.11.0",
    "path": "^0.12.7",
    "serve-favicon": "~2.3.0"
  }
}

I run my node server as: npm run qa2 for example.

I don't know what I am doing wrong. Any help is appreciated

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400

4 Answers4

62

Since you are using windows operating system., the command varies from the unix system command that you are using.

In windows you have to modify you script as.

"scripts": {
    "start": " SET NODE_ENV=development &  node ./bin/server",
    "qa2": "SET NODE_ENV=qa2 & node ./bin/server",
    "prod": "SET NODE_ENV=production & node ./bin/server"
  },

Use SET and then an & after that.

However using cross-env npm package for cross platform stability is recommeded.

Install it like npm install -S cross-env

"scripts": {
    "start": " cross-env NODE_ENV=development &  node ./bin/server",
    "qa2": "cross-env NODE_ENV=qa2 & node ./bin/server",
    "prod": "cross-env NODE_ENV=production & node ./bin/server"
  },
Jagrati
  • 11,474
  • 9
  • 35
  • 56
15

I can suggest cross platform sollution. It's done with the help of the cross-env npm package. Your script section would look like this:

"scripts": {
    "globals" : "npm i -g cross-env",
    "start": "cross-env NODE_ENV=development &  node ./bin/server",
    "qa2": "cross-env NODE_ENV=qa2 & node ./bin/server",
    "prod": "cross-env NODE_ENV=production & node ./bin/server"
  }

So you run once:

npm run globals // to install global dependencies

Then you're free to use your scripts both on linux and windows(mac?).

Lazyexpert
  • 3,106
  • 1
  • 19
  • 33
  • Thanks for the answer, but I think I will go with the other answer. – Shubham Khatri Oct 14 '16 at 11:11
  • Using cross-env is HIGHLY recommended, because you never know who is going to try to run your code. Other developers, servers, virtual boxes, Macs, Linux machines, etc. – Joshua Jan 06 '17 at 22:02
4

Sometimes this can be fixed by using win-node-env if your running on windows, For using it just run the below command.

npm install -g win-node-env
Prasad Gayan
  • 1,424
  • 17
  • 26
0

If you run into this problem in 2021, install cross-env as a dev dependency by running npm i -D cross-env.

Then, modify your command in the package.json file thus:

"scripts": {
    "start": "cross-env NODE_ENV=development node ./bin/server",
    "qa2": "cross-env NODE_ENV=qa2  node ./bin/server",
    "prod": "cross-env NODE_ENV=production node ./bin/server"
  },
Austin
  • 364
  • 1
  • 9
  • 21