74

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?

2
  • Are you asking how to print an environment variable in your shell? Consult the docs for you shell.
    – SLaks
    Jan 14, 2015 at 3:47
  • As @SLaks says, consult the shell docs. A typical example is "echo $NODE_ENV" but shells vary Jan 14, 2015 at 4:03

10 Answers 10

61

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.

4
  • @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?). Jan 14, 2015 at 5:10
  • it shows you nothing because the variable is empty, but it indeed does work. Jan 14, 2015 at 5:38
  • 1
    I defined it by running the app using $ NODE_ENV=production node app Jan 16, 2015 at 3:09
  • 2
    You should do set NODE_ENV=<node env> on Windows and export NODE_ENV=<node env> on unix-based OSes. echo $NODE_ENV worked for me. Nov 25, 2016 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

2
  • 5
    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. Jan 16, 2015 at 3:07
  • 3
    @thetrystero I have the same issue
    – lux
    Jan 28, 2016 at 21:00
24

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.

1
  • 1
    also works from a unix like shell on Windows too Dec 22, 2021 at 16:38
16

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.
2
  • 1
    tried it. process.env.NODE_ENV in the repl returns undefined. Jan 14, 2015 at 4:55
  • 1
    Wouldn't running it as $ NODE_ENV=production node app set the variable to production ? Jan 16, 2015 at 3:09
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.

1
  • 1
    setting it does not work for me in powershell: you should use $env:NODE_ENV="my_node_env"
    – Roel
    Sep 20, 2019 at 9: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
3

To display the current node environment in windows, use:

> echo %NODE_ENV%

It wil output the environment on the command line like:

development
4
  • 37
    It prints "%NODE_ENV%" for me
    – Pumych
    Mar 4, 2016 at 10:00
  • 3
    It helps to find out when you set the env first. Next echo it . other wise it displays same message %NODE_ENV% Try like this :- set NODE_ENV=production and then Echo it you will see the set env
    – Prasad
    Oct 6, 2016 at 6:39
  • 2
    @Pumych try $NODE_ENV
    – AntonIva
    Jul 12, 2017 at 4:20
  • echo $NODE_ENV on Ubuntu 14.04.4 LTS
    – Luckylooke
    Jul 10, 2018 at 12:01
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"
2
  • Yes, I do have these if statements in my app.js. I'm running the app using $ NODE_ENV=production node app Jan 16, 2015 at 3:08
  • @thetrystero So still cann't you echo your NODE_ENV?
    – pmverma
    Jan 16, 2015 at 5:26
1

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.

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.

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