Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript npm Basics (retiring) Installing Packages with npm Managing Dependencies in the package.json File

what is NODE_ENV and why did the teacher mention it in the video?

Not sure where NODE_ENV came from and why it's used

KWAME ADJEI
KWAME ADJEI
4,627 Points

Sorry got carried away hope that helps

1 Answer

KWAME ADJEI
KWAME ADJEI
4,627 Points

NODE_ENV is an environment variable(a dynamic-named value that can affect the way running processes will behave on a computer) that are exposed to the current node process.

In short, it's a variable defined on a running node process that allows you and packages to make decisions based on its value.

Conventional values

  • undefined(default)
  • development
  • production

You can access environment variables by reading from the global process variable. i.e process.env.NODE_ENV

A good real world example of when you might use the NODE_ENV variable could be to use a different database when in development vs production.

var databaseUri = "";
const environment = process.env.NODE_ENV // undefined, production, ...
switch (environment){
 case "development":
     databaseUri = "localhost:27017";
     break;
case "staging": // Value specific to your application. Not recommended for production
     databaseUri = "staging/database/uri";
 case "production":
     databaseUri = "production/database/uri";
     break;
}

The NODE_ENV environment variable is also used by some NPM packages to enforce behaviors.

A good example of this is the debug package which allows you to see all you debug message in development and hides them in production to limit overhead.

How you set the NODE_ENV environment variable varies from operating system to operating system and also depends on your user/project setup.

You can set it as a one-off on Mac and Linux using export NODE_ENV=production or set NODE_ENV=production for Windows.

I personally recommend setting it in your package.json file before your start script. i.e NODE_ENV=production node index.js

Use console.log(process) for the entire list of environment variables.