How to write a good .env.example
One line: .env.example keeps key names and notes, leaves values empty or gives a safe default, and never contains a secret.
What to keep
.env.example is documentation for the next person: which environment variables exist and which are required. Keep the key, a short comment and, for optional keys, a safe default.
An empty value does not say whether the key is required, so mark required keys in a comment.
# Required
DATABASE_URL=
JWT_SECRET=
# Optional (default shown)
PORT=8080
LOG_LEVEL=info
Generating one from a filled file
If you already have a populated .env, choose .env.example in the workbench export. It keeps the keys and drops the values. Nested JSON, YAML and TOML paths become names such as DB_HOST.
# Source
DB_HOST=db.internal
DB_PASSWORD=EXAMPLE_ONLY_pw
# .env.example
DB_HOST=
DB_PASSWORD=
Comments and ordering
Group keys by feature and separate the groups with a one-line comment. Sorting alphabetically gives clean diffs but diverges from the real file's order.
For keys that change per environment, leave the allowed examples in a comment so a deploy does not pick the wrong one.
Catching missing keys in CI
Compare the keys in .env.example with the keys present in the deployment environment. A short script that sorts both lists and prints the difference is enough.
Make sure the script prints key names only, never values.
# Key names only (values are never read)
comm -3 <(cut -d= -f1 .env.example | sort) <(cut -d= -f1 .env | sort)
Checklist
Before you share the file, confirm:
- no real value remains
- every required key is present
- new keys from this change are included
- no secret hides behind a public prefix such as NEXT_PUBLIC_
Questions
Can .env.example contain defaults?
Non-secret defaults are fine, but avoid production values that would work if copied by mistake.
Should I split it per environment?
One shared .env.example with per-environment notes, or a .env.staging.example, is easier to maintain than many near-duplicates.
Sources
Every example here can be pasted into the workbench as it is.
Open the workbench