What is NODE_ENV and how to use it in Express?
This is my app that I'm currently running on production.
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());
});
Now I've learned about NODE_ENV
and want to use it. How can I do this?
Solution 1:
NODE_ENV
is an environment variable made popular by the express web server 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:
var environment = process.env.NODE_ENV
Assume production if you don't recognise the value:
var isDevelopment = environment === 'development'
if (isDevelopment) {
setUpMoreVerboseLogging()
}
You can alternatively using express' app.get('env')
function, but note that this is NOT RECOMMENDED as it defaults to "development"
, which may result in development code being accidentally run in a production environment - it's much safer if your app throws an error if this important value is not set (or if preferred, defaults to production logic as above).
Be aware that if you haven't explicitly set NODE_ENV
for your environment, it will be undefined
if you access it from process.env
, there is no default.
Setting NODE_ENV
How to actually set the environment variable varies from operating system to operating system, and also depends 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 isn'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 dictated that there are two 'main' values you should use for NODE_ENV
, either production
or development
, all lowercase. There's nothing to stop you from using other values, (test
, for example, if you wish to use some different logic when running automated tests), but be aware that if you are using third-party modules, they may explicitly compare with 'production'
or 'development'
to determine what to do, so there may be side effects that aren't immediately obvious.
Finally, 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 be applied to the process from which it was set, so things probably won't work like you'd expect them to. Don't do it - you'll regret it.
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 included 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.