Node.js Course
Node.js
/
Intermediate

process.env

Definition

A property of the global `process` object containing the user environment. It is the standard way to pass secret configuration (like API keys) to a Node.js app.

Explain Like I'm New

You NEVER hardcode your database passwords or Stripe API keys in your code, because if you upload it to GitHub, hackers will steal them. Instead, you put secrets inside a hidden `.env` file on your computer. When Node runs, it reads that file and injects the secrets into `process.env`.

Real World Example

Connecting to a database: `mongoose.connect(process.env.DATABASE_URL)`.

Common Use Cases

  • •Storing secrets safely
  • •Switching between Development, Staging, and Production databases dynamically

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What popular NPM package is used to load a `.env` file into `process.env`?

intermediate

  • What is `process.env.NODE_ENV` conventionally used for?

advanced

  • Should you ever commit a `.env` file to a public git repository?

Flash Cards

Question

What npm package is used?

Click to reveal answer
Answer

`dotenv`. By calling `require('dotenv').config()`, it magically reads the `.env` file and populates the `process.env` object.

Question

What is NODE_ENV used for?

Click to reveal answer
Answer

It is a standard convention used to define the environment type. Typically it is set to `'development'`, `'production'`, or `'test'`. Frameworks like Express read this variable; if it's set to 'production', Express automatically caches views and hides stack traces to run 3x faster.