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

Javascriptnode.jsExpress

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

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

Solution 3 - Javascript

I'd like to address a straightforward way to passing NODE_ENV variables to your node script in order to access them in process.env

  "scripts": {
    "start": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon server.js",
    "debug": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon --debug server.js",
    "test": "./node_modules/.bin/cross-env NODE_ENV=test ./node_modules/.bin/babel-tape-runner test/test-*.js"
  },

can be used as

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

Solution 4 - Javascript

The can detect which environment you are in by inspecting app.settings.env.

Solution 5 - Javascript

cannot access the nodejs server. can detect node env from express using app.setting.env

  1. var app = express();
  2. app.setting.env render to the template engine.
  3. check from the browser.

Solution 6 - Javascript

There are a lot of useful recommendations in other answers. I'm generally doing it like this:

const environment = process.env.NODE_ENV || 'development';

The good thing is that such approach is not specific to Express per se, but actually is an accepted practice in wider Node.js ecosystem.

Also, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod.

Please see more details and use cases on the detect-environment's page.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsethvargoView Question on Stackoverflow
Solution 1 - JavascriptalessioalexView Answer on Stackoverflow
Solution 2 - JavascriptDB PrasadView Answer on Stackoverflow
Solution 3 - JavascriptJeff VossView Answer on Stackoverflow
Solution 4 - JavascriptDHamrickView Answer on Stackoverflow
Solution 5 - JavascriptZILONG PANView Answer on Stackoverflow
Solution 6 - JavascriptSlava Fomin IIView Answer on Stackoverflow