Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.