Guide
Keep .env and .env.example in sync, and find missing keys
A teammate adds `REDIS_URL` to `.env.example`, you pull, and the app crashes on a value you don’t have. Here’s how to catch missing keys before they bite.
By Anmol Sethi · Updated
01
What .env.example is for
It lists every variable the app needs, with empty or fake values, and it’s committed to git. Your real .env should have every key it declares.
02
Find missing keys
Compare the key names of both files (macOS zsh or bash):
keys() { grep -oE '^[A-Za-z_][A-Za-z0-9_]*=' "$1" | sort -u; }
comm -23 <(keys .env.example) <(keys .env)Each line it prints, like REDIS_URL=, is a key your .env lacks. Swap -23 for -13 to list keys in .env that the example doesn’t document.
Lines written as export KEY=value aren’t matched by this pattern. Remove the export prefixes first if your files use them.
03
Rebuild the example from your .env
Copy every key with its value blanked out, keeping comments:
sed -E 's/^([A-Za-z_][A-Za-z0-9_]*)=.*/\1=/' .env > .env.exampleRead the result before committing it: a value split across lines would survive.
04
Check before every run
Turn the comparison into a check that stops early:
missing=$(comm -23 <(keys .env.example) <(keys .env))
[ -z "$missing" ] || { echo "Missing in .env:"; echo "$missing"; exit 1; }Save it as scripts/check-env.sh and run it from a predev script in package.json, or your framework’s equivalent.
05
The one-click way: Dotvane
Dotvane reads .env.example (or .sample, .template, .dist) for every project and names any key the active profile lacks, like “Missing REDIS_URL”, right in the menu bar, as soon as you pull.
Try Dotvane free for 7 days. Every feature, no account.
Download for Mac