Writing a Good .env.example
Your .env.example is the contract between your code and everyone who runs it. This page
shows two versions of the same file. Both are valid, but the second one is what dotenv-diff suggests
as a best practice for your team.
Minimal version
The bare minimum: every key your code uses, with empty or placeholder values.
This is enough for dotenv-diff to detect missing and unused variables — but a new contributor still has no idea what any of these keys are for, or where to get a value.
Documented version
The same file, written so it is easy to understand for new contributors to the team.
Every key above is documented, so this file passes dotenv-diff --comment-warnings.
Annotations
Two keys in the example carry an annotation as well:
NODE_ENVis marked@optional, so dotenv-diff does not report it as missing when you leave it out of your.env— the code has a sensible default for it.PARTNER_API_TOKENhas an@expiredate, so dotenv-diff warns you when the token is close to expiring — and fails the build when it has less than 7 days left.
Note that an annotation on its own is not documentation: the real comment above it is what makes the key documented.
Finding undocumented keys
Comment warnings are opt-in. Enable them to flag every .env.example key that has
no documenting comment — either a # comment on the line directly above, or an
inline # comment after the value.
Or enable it permanently in your config file:
Best Practices
- Never commit real secrets — keep values empty or use obvious placeholders.
- Keep safe defaults (ports, local URLs) so the project runs out of the box.
- Explain why a key exists and where to get a value, not just what it is called.
- Group related keys together and keep the order stable to keep diffs readable.
- Mark keys with a code-level default as
@optional. - Add
@expireto every time-limited token or credential.
See also
- Expiration Warnings —
@expiresyntax and thresholds. - Optional Keys —
@optionalsyntax and behavior. - Comment Warnings — what counts as a documented key.