50

So, in my node.js 5.2.0 / express.js 4.2.0 I can do

if (app.get('env') === 'development') {
  app.use(//etc

or

var env = process.env.NODE_ENV || 'development';
if (env === 'development') {
  app.use(//etc

So process.env.NODE_ENV and app.get('env') both get the environment's value. Is there any significant difference besides the syntax?

Thanks

slevin
  • 4,166
  • 20
  • 69
  • 129
  • There is an already accepted answer of mine, here: https://stackoverflow.com/questions/54770052/warning-node-env-value-of-test-did-not-match-any-deployment-config-file-name/61135173#61135173 By default, `app.get('env')` returns `development` and `process.env.NODE_ENV` returns `undefined`. If you want to set both at the same time and update the environments, you may refer to the above link. – Santhosh John Jun 28 '20 at 05:52

1 Answers1

76

There is no significant difference.

Express app.get('env') returns 'development' if NODE_ENV is not defined. So you don't need the line to test its existence and set default.

aleung
  • 9,848
  • 3
  • 55
  • 69