Environment Variables
Environment variables are settings stored outside your code. The terminal knows them, and every program it runs can read them. They are the standard way to store things that differ between machines: database addresses, API keys, port numbers, and mode flags. Your code stays the same while the environment around it changes.
Node.js exposes them through process.env, an object of key-value pairs. Read one with process.env.PORT or process.env.PORT, and get undefined if it is not set. This is why you never hard-code secrets into source files: put the value in an environment variable instead, and the same code works on your laptop and on a server with different values.
Setting a variable is different on each platform. On Windows PowerShell and cmd, set it with set PORT=3000 before running your script. On Mac and Linux, type PORT=3000 before the node command. To keep variables easy to manage, the dotenv package reads a .env file and loads it into process.env automatically.
A .env file is a plain text list of KEY=VALUE lines, usually one per line. It belongs in the same folder as your project but never in version control, because it contains exactly the secrets you want to keep private. Add .env to your .gitignore file and create a .env.example with empty values so teammates know what to fill in.
Reading configuration with process.env
Read a variable with process.env.NAME. If it is missing you get undefined, so provide a fallback with a default value. This pattern, value or default, keeps a script running even when a variable was not set.
const port = process.env.PORT || 3000;
console.log("Server will listen on port", port);
if (process.env.NODE_ENV === "production") {
console.log("Running in production mode");
}
Setting variables
On Windows, use set before the command. On Mac and Linux, put the variable before the command. The variable only lives for that one command, which is exactly what you want for a quick test.
# Windows (cmd or PowerShell)
set PORT=3000
node server.js
# Mac / Linux
PORT=3000 node server.js
Using a .env file with dotenv
The dotenv package loads a .env file at startup. Install it, require it as early as possible, and then process.env contains everything from the file. This keeps all configuration in one visible place without pasting secrets into every launch command.
npm install dotenv
// at the very top of your main file
require("dotenv").config();
const port = process.env.PORT || 3000;
Syntax
# .env
PORT=3000
API_KEY=sk-secret-here
# server.js
require("dotenv").config();
const port = process.env.PORT || 3000;
console.log("Listening on", port);
Never Commit .env with Secrets
.env with API keys in git leaks secrets forever — add .env to .gitignore and use process.env.
require("dotenv").config()
Good to know
- process.env holds environment variables as key-value pairs.
- Use a default so a missing variable does not crash the script.
- Never put secrets in code; use environment variables or .env.
- dotenv loads a .env file into process.env at startup.
- Add .env to .gitignore so keys never reach version control.