Node.js Course
Node.js
/
Beginner

Environment Variables

Definition

Dynamic values that affect the way running processes will behave on a computer. They are heavily used to store sensitive information and configuration data outside of the application's source code.

Explain Like I'm New

Imagine building a robot that connects to a database. You don't want to weld the database password permanently into the robot's brain (source code), because if you want to sell the robot to someone else, they can't change the password. Instead, you put a sticky note on the robot's back. When the robot turns on, it reads the sticky note (Environment Variable) to know the password.

Real World Example

Using a `.env` file to store `DATABASE_URL`, `STRIPE_SECRET_KEY`, and `NODE_ENV`. The `.env` file is heavily excluded from Git via `.gitignore` so secrets never leak to GitHub.

Common Use Cases

  • •Security
  • •Configuring dev/staging/prod environments

Interactive Example

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

Interview Questions

basic

  • What object in Node.js contains the environment variables?

intermediate

  • Why do you need the `dotenv` package?

Flash Cards

Question

What object?

Click to reveal answer
Answer

`process.env`

Question

Why use dotenv?

Click to reveal answer
Answer

Because standard Node.js doesn't know how to read a `.env` text file automatically. The `dotenv` package parses the file and artificially injects the values into `process.env` for you.