70

Please, answers here all refer to a something called process.env.NODE_ENV, but echo $process.env.NODE_ENV from the command line did not work. Any ideas?

Basant Singh
  • 5,736
  • 2
  • 28
  • 49
thetrystero
  • 5,622
  • 5
  • 23
  • 34

10 Answers10

58

Use echo $NODE_ENV. The command line is a shell, probably bash and that's the bash syntax to print the value of an environment variable.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • @thetrystero Based on your various comments, the environment variable probably hasn't been defined anywhere. What value are you expecting it to have? When and where are you attempting to set it? Also note that it is not a default variable for Node ([What is NODE_ENV in Express?](http://stackoverflow.com/questions/16978256/what-is-node-env-in-express)). – Jonathan Lonowski Jan 14 '15 at 05:10
  • it shows you nothing because the variable is empty, but it indeed does work. – Peter Lyons Jan 14 '15 at 05:38
  • 1
    I defined it by running the app using `$ NODE_ENV=production node app` – thetrystero Jan 16 '15 at 03:09
  • 2
    You should do `set NODE_ENV=` on Windows and `export NODE_ENV=` on unix-based OSes. `echo $NODE_ENV` worked for me. – Teleporting Goat Nov 25 '16 at 17:04
45

If you have defined NODE_ENV variable then you should be able to see this by typing node in the command prompt which will open the node cell and then type process.env.NODE_ENV.

To check the existing env variables .. type this .. process.env

Blowsie
  • 40,239
  • 15
  • 88
  • 108
user2720864
  • 8,015
  • 5
  • 48
  • 60
  • 4
    I've run `NODE_ENV=production node app` from the command line, so this should set it to production correct? then in another terminal window within the same directory where i ran the above command, i enter the node REPL and type `process.env.NODE_ENV` but it returns undefined. – thetrystero Jan 16 '15 at 03:07
  • 2
    @thetrystero I have the same issue – lux Jan 28 '16 at 21:00
22

You call list all variables for Macs available for your project directory with...

printenv

I use this often to look for the NODE_ENV and other variables.

Mark A
  • 1,995
  • 4
  • 18
  • 26
14

go to node REPL, and then give process.env.NODE_ENV and the variable process is scoped inside nodejs process, not in your shell process.

sk3037@saravana:~/src$ node
> process.env.
mscdex
  • 104,356
  • 15
  • 192
  • 153
7

Setp-by-step windows CMD NODE_ENV:

  1. set NODE_ENV=my_node_env (defines NODE_ENV)

  2. node (run node)

  3. process.env.NODE_ENV (show NODE_ENV)

After "set NODE_ENV" you can run the application, and it will use the set NODE_ENV. You can run your application with custom environment in pm2 without problem.

Pumych
  • 1,368
  • 3
  • 18
  • 31
  • 1
    setting it does not work for me in powershell: you should use $env:NODE_ENV="my_node_env" – Roel Sep 20 '19 at 09:16
4
  1. Find the Id of the process you are running by executing ps aux | grep node
  2. Look at the environment variables used by that process by executing less /proc/[your-id]/environ
brainsiq
  • 41
  • 2
3

To display the current node environment in windows, use:

> echo %NODE_ENV%

It wil output the environment on the command line like:

development
stephan
  • 655
  • 1
  • 7
  • 11
1

Have you set the NODE_ENV for process?

Here are some example. Somewhere in code, you set the node environment to "production" or "development" or "any thing you want". And do some stuff according to your node environment.

process.env.NODE_ENV="production";

//others coding
if(process.env.NODE_ENV === "production")
{
  //useblabla log level.
  //use production log.
}
else if(process.env.NODE_ENV === "development")
{
  //useblabla log level.
  //use development log.

}
console.log(process.env.NODE_ENV); //"production"
pmverma
  • 1,653
  • 13
  • 27
0

Run npm config get production; if it's true, then it's in production.

Run npm config get dev; if it's true, then it's in development.

Run npm config ls -l to view all default cli configs value.

Sunil Kumar Das
  • 372
  • 1
  • 2
  • 12
0

I did this by defining the production environment in my scripts in the package.json file, and based on the script that I run, console.log() will print out the name of the environment:

// ... other package.json stuff
"scripts": {
    "dev": "NODE_ENV=development nodemon app.js",
    "start": "NODE_ENV=production nodemon app.js"
 },

If I run npm start the value of process.env.NODE_ENV will be production, whereas if I run npm run dev, it's value will be development.

Gil
  • 21
  • 4