Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: How does process.env differ from global?

How is setting an environment variable like process.env.thing = 42 different from creating a global variable like global.thing = 42?

When would prefer process.env.thing over global? What are the pros/cons of both objects?

like image 586
sdfsdf Avatar asked Oct 17 '18 19:10

sdfsdf


1 Answers

global is the global object. process is available globally, because it is a property of global. In fact:

global.process === process //-> true

process.env has properties set to the environment variables of the system. These can be set a variety of ways outside of node itself, and read in by accessing properties of process.env.

At the command line try:

FOO=bar node -e "process.env.FOO"

The process module is just a globally available thing.

like image 194
Alex Wayne Avatar answered Sep 22 '22 08:09

Alex Wayne