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


Question

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...

1
51
4/21/2015 10:24:44 AM

Accepted Answer

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).

66
12/9/2011 7:48:27 PM

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

if ( app.get('env') === 'development' ) {
    app.use(express.errorHandler());
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon