60

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

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
sethvargo
  • 26,739
  • 10
  • 86
  • 156

6 Answers6

76

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

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 1
    thanks! I was running in development, so NODE_ENV was never present. I don't have access to `app` inside the code I'm writing, but checking for NODE_ENV and defaulting to `development` works – sethvargo Dec 09 '11 at 23:20
  • How can I do it from a Gruntfile.js? I tried both but app is not defined and process.env.NODE_ENV is undefined. There is a way? – Vadorequest Nov 27 '13 at 19:07
  • It's better to have it setup in `bashrc`: http://www.hacksparrow.com/running-express-js-in-production-mode.html – alessioalex Nov 27 '13 at 20:21
26

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

if ( app.get('env') === 'development' ) {
    app.use(express.errorHandler());
}
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
DB Prasad
  • 785
  • 7
  • 9
  • 13
    But how does it know? Where does express look to find out what 'env' has been set to. I haven't set it in my app's code. – Costa Michailidis Nov 20 '14 at 23:15
  • 3
    @Costa it's set in the Unix shell as the `NODE_ENV` environment variable. Note **the term 'env' here is confusingly overloaded**: the Unix environment, where settings for apps are sometimes stored, as well as the environment setting NODE_ENV which determines production, development etc. – mikemaccana Apr 21 '15 at 10:39
  • 1
    @mikemaccana, Ah, got it. The term "overloaded" explains a lot there. Thanks very much! – Costa Michailidis Apr 21 '15 at 15:04
  • 3
    I'm curious about how this is implemented. Is ´app.get´ the same function you use to define a route? Doesn't sound great. – ichigolas Jul 20 '16 at 14:26
13

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());
}
Jeff Voss
  • 3,637
  • 8
  • 46
  • 71
2

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

DHamrick
  • 8,338
  • 9
  • 45
  • 62
  • 1
    this doesn't work if you don't have access to express (like in a custom lib module) – sethvargo Dec 09 '11 at 23:19
  • 3
    @sethvargo The environment would need to be injected into the module, either via constructor or method injection. e.g. myModule.doSomething(app.settings.env). If this module is lower level code being called from higher level code, then the value needs to get passed down the call stack. – Jarrett Meyer Dec 12 '14 at 22:17
0

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.
Simon H
  • 2,495
  • 4
  • 30
  • 38
ZILONG PAN
  • 6,305
  • 1
  • 11
  • 6
0

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.

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202