Lesson 21 / 30
.env Files
Keep secrets and config out of code with .env files, dotenv or Node's built-in --env-file flag.
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_123Loading it
Node 20+ can read a .env file natively — no package required.
node --env-file=.env server.jsNever commit it
Add .env to .gitignore — commit a .env.example with placeholder keys instead.