Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you detect the environment in an express.js app?

How do you detect what environment an expressJS app is running in? (development, test, production?). There's nothing in process.env indicating an environment...

I'm aware that you can declare variables in your configuration file under each environment, but that doesn't help if you are dynamically loading modules...

like image 489
sethvargo Avatar asked Dec 09 '11 17:12

sethvargo


People also ask

How do you check node environment?

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 .

How do I read an environment variable in node JS?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.

What is an environment in JavaScript?

The data structure that provides that storage space is called an environment in JavaScript. It maps variable names to values. Its structure is very similar to that of JavaScript objects. Environments sometimes live on after you leave their scope. Therefore, they are stored on a heap, not on a stack.


2 Answers

You can either check the environment by checking the app.settings.env (this will work in Express), or you can do it in a more direct way by checking process.env.NODE_ENV (the environment is the one found in that variable or 'development' by default < this also works in other libraries such as Socket.IO etc).

like image 168
alessioalex Avatar answered Sep 25 '22 09:09

alessioalex


app.get('env') would also return the environment.

if ( app.get('env') === 'development' ) {     app.use(express.errorHandler()); } 
like image 37
DB Prasad Avatar answered Sep 26 '22 09:09

DB Prasad