Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recognize NODE_ENV in Node.js?

Tags:

I'm using Express for framework.

And I would like to divide my configuration to "development" and "production".

I know that I can use

app.configure('development', function() {}); app.configure('production', function() {}); 

But I want to know the way actually how I can know what the NODE_ENV value is.

I tried to find in global variables but I could not find.

I really need this to use other Database configuration depends on NODE_ENV

in my database config.js file.

like image 986
jwchang Avatar asked Mar 02 '12 02:03

jwchang


People also ask

What is NODE_ENV in Node JS?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).

What does NODE_ENV default to?

We see that it in fact reads NODE_ENV and defaults to 'development' if it isn't set. This variable is exposed to applications via 'app. get(“env”)' and can be used to apply environment specific configurations as explained above, but it's up to you to use this or not.

Can you override NODE_ENV?

You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production.

How do I read an environment variable in Node JS?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.


2 Answers

I have found the answer

process.env.NODE_ENV 
like image 91
jwchang Avatar answered Sep 28 '22 00:09

jwchang


Express also exposes this data via app.settings.env

like image 41
Dominic Barnes Avatar answered Sep 27 '22 23:09

Dominic Barnes