Dev Basics · 7 min read
Environment Variables: .env Best Practices
Environment variables are the standard way to give an application its configuration: database URLs, API keys, feature flags, the port to listen on. The idea, popularized by the Twelve-Factor App methodology, is that the same build runs in development, staging and production, and only the environment changes. It is a simple mechanism that is easy to use carelessly.
This guide covers what belongs in environment variables, .env files, keeping secrets out of Git and out of front-end bundles, validating configuration at start-up, and when to move to a secret manager.
Try it yourself with the related tool.
Convert .env and JSON →Advertisement
What belongs in the environment
Anything that differs between deployments belongs in configuration rather than code: credentials, hostnames, ports, external service URLs, log levels and feature toggles. Anything that is the same everywhere, such as business rules or default page sizes, belongs in code, where it is versioned and reviewed. A useful test is whether you could open-source the codebase right now without leaking a credential.
.env files
A .env file holds KEY=value lines that a library such as dotenv, or the framework itself, loads into the process environment during development. Commit a .env.example listing every variable with placeholder values and a comment on each, so new developers know what to set. Add .env itself to .gitignore before the first commit. If a real secret is ever committed, treat it as leaked and rotate it — deleting it in a later commit does not remove it from history, and public repositories are scanned for keys within minutes.
Front-end variables are public
Frameworks expose selected variables to browser code at build time, using a prefix such as NEXT_PUBLIC_, VITE_ or REACT_APP_. Those values are written into the JavaScript bundle and anyone can read them with the browser's developer tools. Only put genuinely public values there: an analytics ID, a public API base URL, a publishable payment key. Secret keys must stay on a server and be used through your own API endpoint.
Validate at start-up
Read and validate all configuration in one place when the application starts, and fail immediately with a clear message if something is missing or malformed. A service that starts without its database URL and crashes on the first request — or worse, silently falls back to a default — is much harder to debug. Schema libraries such as Zod or Pydantic make this concise and give you typed configuration objects instead of scattered process.env lookups.
Types, names and formatting
Every environment variable is a string. "false" is a non-empty string and therefore truthy in JavaScript, which has caused more than one feature flag to stay on unexpectedly; parse booleans and numbers explicitly. Use upper-case names with underscores and a consistent prefix per service. Quote values containing spaces or # in .env files, and remember that different loaders treat quotes, multi-line values and variable expansion differently.
Where environment variables fall short
Environment variables are visible to every process the application starts, can appear in crash reports and debug pages, and are often printed by careless logging. In container platforms, anyone who can inspect the container may see them. For sensitive production secrets, a secret manager — AWS Secrets Manager, Google Secret Manager, Azure Key Vault, HashiCorp Vault, Doppler and similar — adds access control, audit logs and rotation. Many teams use the secret manager as the source and inject values as environment variables at start-up, getting both convenience and control.
A short checklist
Keep .env out of Git and provide .env.example. Never put secrets behind a public prefix. Validate configuration on start-up. Use separate credentials per environment so a leaked development key cannot touch production. Never log the full environment. And rotate any secret that has been shared in chat, pasted into a ticket or committed, however briefly.

