Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.env vs app.get('env') on getting the express.js environment

So, in my node.js 5.2.0 / express.js 4.2.0 I can do

if (app.get('env') === 'development') {   app.use(//etc 

or

var env = process.env.NODE_ENV || 'development'; if (env === 'development') {   app.use(//etc 

So process.env.NODE_ENV and app.get('env') both get the environment's value. Is there any significant difference besides the syntax?

Thanks

like image 834
slevin Avatar asked Dec 11 '15 15:12

slevin


People also ask

What is process env in Express?

The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process. env.

What is process env Javascript?

js, process. env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process. env during runtime and can later be accessed.

Why do we use process env?

So instead, you use process. env. PORT to tell your application to take the PORT by reading the Environment Variable. You put the || just to make sure that if the PORT variable by any chance was not found, use the specified port instead.


1 Answers

There is no significant difference.

Express app.get('env') returns 'development' if NODE_ENV is not defined. So you don't need the line to test its existence and set default.

like image 50
aleung Avatar answered Oct 05 '22 22:10

aleung