What is NODE_ENV in Express?

What is NODE_ENV in Express?

var app = express();
app.set(‘views’,settings.c.WEB_PATH + ‘/public/templates’);
app.set(‘view engine’,’ejs’);
app.configure(function(){
app.use(express.favicon());
app.use(express.static(settings.c.WEB_PATH + ‘/public’));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({
cookie:{ domain:”.”+settings.c.SITE_DOMAIN, maxAge:1440009999},
secret:’hamster’,
store: r_store,
}));
app.use(useragent.express());
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
});

This is my app. I’m currently running it in production.
However, someone told me about NODE_ENV. Do I have to add it to this code? How do I add it?

Solutions/Answers:

Solution 1:

NODE_ENV is an environment variable made popular by the express webserver framework. When a node application is run, it can check the value of the environment variable and do different things based on the value. NODE_ENV specifically is used (by convention) to state whether a particular environment is a production or a development environment. A common use-case is running additional debugging or logging code if running in a development environment.

Accessing NODE_ENV

You can use the following code to access the environment variable yourself so that you can perform your own checks and logic:

Related:  Object.watch() for all browsers?

var environment = process.env.NODE_ENV

Or alternatively using express’ app.get('env') (note: this defaults to "development")

Be aware that if you haven’t explicitly set NODE_ENV for your environment, it will be undefined.

Setting NODE_ENV

How to actually set the environment variable varies from operating system to operating system, and also depend on your user setup.

If you want to set the environment variable as a one-off, you can do so from the command line:

  • linux & mac: export NODE_ENV=production
  • windows: $env:NODE_ENV = 'production'

In the long term you should persist this so that it doesn’t unset if you reboot – rather than list all the possible methods to do this, I’ll let you search how to do that yourself!

Convention has dictacted that there are only two values you should use for NODE_ENV, either production or development, all lowercase. There’s nothing stopping you adding more values, but it’s probably not a good idea, as I see a lot of this sort of code in many of the node_modules that I use:

var development = process.env.NODE_ENV !== 'production';

Note that it’s a really bad idea to try to set NODE_ENV from within a node application itself – if you do it will only apply to the process from which it was set, so things probably won’t work like you expect them to. Don’t do it – you’ll regret it.

Related:  how to display data values on Chart.js

Solution 2:

NODE_ENV is an environmental variable that stands for node environment in express server.

It’s how we set and detect which environment we are in.

It’s very common using production and development.

Set:

export NODE_ENV=production

Get:

You can get it using app.get('env')

Solution 3:

I assume the original question was how does Express use this environment variable.

Express uses NODE_ENV to alter its own default behavior. For example, in development mode, the default error handler will send back a stacktrace to the browser. In production mode, the response is simply Internal Server Error, to avoid leaking implementation details to the world.