Andy Jarrett // Code. Develop. Manage.

How does express.js know its in live or development?

I've seen this following IF logic before, even used it, but with having no Node.js actively live I've never known exactly how it "knows"

// development only
if ('development' == app.get('env')) app.use(express.errorHandler());

A little dig through Express.js on Github and it seems that it comes down to whether a process.env.NODE_DEV is set. Look around line 50 (look for)

this.set('env', process.env.NODE_ENV || 'development');

To test this I wrote a simple node app called "whatEnvironment.js" with one console.log line and it returned "undefined"

A quick change the to the call (to set the environment variable) $ NODE_ENV=Production node whatEnvironment.js and this returned "Production"

When it comes to splitting out your servers (Dev, Staging, and Live) you can set NODE_ENV={environment} in the machine's ~/.bash_profile (for Mac)


I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee.
Your support keeps this site running and the coffee brewing! ☕️