Lesson 20 / 28

.env Files

Keep secrets and per-environment config out of your code.

A .env file

Store key=value pairs in a .env file, load them with the dotenv package (or Node's built-in --env-file flag).

# .env
PORT=4000
DB_URL=postgres://localhost:5432/app
API_KEY=sk_test_123

Loading it

Node 20+ can read a .env file natively — no package required.

node --env-file=.env server.js

Never commit it

Add .env to .gitignore — commit a .env.example with placeholder keys instead.