Basics
What is a .env file?
A .env file stores environment variables used by an application. These values often include API URLs, database URLs, feature flags, ports, runtime modes, and service credentials. Dotenv-style files are common in Node.js, Next.js, Vite, Python/FastAPI, Docker, and CI/CD workflows because they keep configuration separate from source code.
Setup
How to create a .env file
Create a file named .env in your project root, add one KEY=value pair per line, and use clear variable names that describe what each value controls. Keep comments short, avoid spaces in keys, quote values only when needed, and create a matching .env.example file so teammates know which variables are required without seeing real secrets.
- Use one variable per line.
- Prefer uppercase keys with underscores, such as DATABASE_URL.
- Keep real local values in .env.
- Create .env.example with the same keys and empty values.
- Restart the app if your framework only reads env files at startup.
Comparison
.env vs .env.example
.env contains real local values and may include secrets. .env.example documents required variables without real secrets, which makes it safer to commit and useful for onboarding. A good project usually keeps .env out of Git, keeps .env.example in Git, and updates the example whenever a required variable changes.
Validation
What this .env validator checks
A dotenv validator is most useful when it catches mistakes before they reach the app runtime. This generator highlights duplicate keys, empty required values, invalid key names, browser-exposed secret risks, and syntax patterns that are easy to miss during setup.
- Duplicate keys that can override earlier values.
- Missing values that may fail during boot or deployment.
- Invalid names with spaces or unsupported characters.
- Public prefixes such as NEXT_PUBLIC_ or VITE_ used with private-looking secrets.
- A mismatch between real .env values and the safer .env.example template.
Next.js
Next.js environment variables
In Next.js, environment variables are server-only by default. Variables prefixed with NEXT_PUBLIC_ can be exposed to browser-side code, so they should only contain values that are safe for users to see, such as a public app URL. Never put API secrets, private tokens, database passwords, payment keys, or signing keys in NEXT_PUBLIC_ variables.
Frameworks
Vite, frontend, and public env prefixes
Frontend build tools often require a public prefix before a variable can be used in browser code. Vite commonly uses VITE_, while Next.js uses NEXT_PUBLIC_. Treat any public-prefixed value as visible to users and reserve private credentials for server-side code, backend services, or a secret manager.
Docker
Docker and environment variables
Docker Compose can use environment values through environment entries and env_file options. This is useful for containers, databases, worker services, and local development stacks. Keep Docker env files organized by service, avoid mixing local-only secrets with production deployment secrets, and check that required container variables match the app documentation.
Mistakes
Common .env mistakes
The most common .env problems are small formatting and workflow mistakes that are hard to notice during development but painful during deploys.
- Committing .env to Git.
- Using duplicate keys that silently override each other.
- Leaving required variables with missing values.
- Adding spaces around keys or using invalid key names.
- Exposing secrets through NEXT_PUBLIC_, VITE_, or other browser-visible prefixes.
- Using inconsistent variable names across local, staging, and production.
- Forgetting to update .env.example when a required variable changes.
Security
Security checklist
Treat environment files as sensitive project configuration. Local .env files are convenient, but production systems often need stronger secret handling.
- Add .env to .gitignore.
- Do not share secrets in chat, tickets, screenshots, or recordings.
- Rotate API keys and passwords if they are exposed.
- Use secret managers for production where possible.
- Use .env.example to document required variables without real values.
- Keep browser-exposed variables separate from private server variables.
Example
Example .env file
A safe development .env example might include APP_ENV=development, APP_URL=http://localhost:3000, DATABASE_URL=postgres://user:password@localhost:5432/app, API_BASE_URL=https://api.example.com, and FEATURE_FLAGS=true. Replace example values with your own local configuration and keep real secrets out of shared files.
Use cases
When should you use this tool?
Use this tool when you are starting a new app, documenting required variables, preparing a Docker Compose project, checking for duplicate or missing env keys, creating a safe .env.example, reviewing a framework migration, or preparing a deployment checklist before release.
Workflow
Deployment checklist for environment variables
Before deploying, compare .env.example with the real variables configured in your hosting platform, CI/CD system, Docker service, or secret manager. The goal is to make sure every required value exists in the right environment without copying local-only secrets into production.
- Confirm required variables exist for local, staging, and production.
- Keep production secrets in the hosting platform or secret manager.
- Rotate any key that was copied into a public place.
- Document new variables in .env.example during the same change.
- Restart or redeploy services after changing runtime variables.
Trust signal
Privacy and local processing
This tool runs in your browser and does not store your environment variables. It is designed for generation and validation only, so production secrets should still be managed through your deployment platform or secret manager.