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?

- 5,736
- 2
- 28
- 49

- 5,622
- 5
- 23
- 34
-
Are you asking how to print an environment variable in your shell? Consult the docs for you shell. – SLaks Jan 14 '15 at 03:47
-
As @SLaks says, consult the shell docs. A typical example is "echo $NODE_ENV" but shells vary – Mark Dickson Jr. Jan 14 '15 at 04:03
10 Answers
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.

- 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
-
1I defined it by running the app using `$ NODE_ENV=production node app` – thetrystero Jan 16 '15 at 03:09
-
2You should do `set NODE_ENV=
` on Windows and `export NODE_ENV= – Teleporting Goat Nov 25 '16 at 17:04` on unix-based OSes. `echo $NODE_ENV` worked for me.
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

- 40,239
- 15
- 88
- 108

- 8,015
- 5
- 48
- 60
-
4I'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
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,995
- 4
- 18
- 26
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.

- 104,356
- 15
- 192
- 153
-
1
-
1Wouldn't running it as `$ NODE_ENV=production node app` set the variable to `production` ? – thetrystero Jan 16 '15 at 03:09
Setp-by-step windows CMD NODE_ENV:
set NODE_ENV=my_node_env (defines NODE_ENV)
node (run node)
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,368
- 3
- 18
- 31
-
1setting it does not work for me in powershell: you should use $env:NODE_ENV="my_node_env" – Roel Sep 20 '19 at 09:16
- Find the Id of the process you are running by executing
ps aux | grep node
- Look at the environment variables used by that process by executing
less /proc/[your-id]/environ

- 41
- 2
To display the current node environment in windows, use:
> echo %NODE_ENV%
It wil output the environment on the command line like:
development

- 655
- 1
- 7
- 11
-
36
-
3It 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 06 '16 at 06:39
-
2
-
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"

- 1,653
- 13
- 27
-
Yes, I do have these if statements in my app.js. I'm running the app using `$ NODE_ENV=production node app` – thetrystero Jan 16 '15 at 03:08
-
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.

- 372
- 1
- 2
- 12
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.

- 21
- 4