How to Create and Use .env Files (2026)
Environment variables keep sensitive configuration out of your source code. API keys, database credentials, third-party tokens, and deployment-specific settings all belong in .env files rather than hardcoded in your application. This guide covers .env file format, naming conventions, loading strategies across languages, and security best practices.
- Quote any value containing #, spaces or line breaks: dotenv and Node cut unquoted values at #.
- Every value is a string; "false" is truthy in JavaScript.
- Node 20.6+ reads .env files natively with --env-file.
- Docker Compose uses .env to fill ${VARS} in compose.yaml, not to set container variables.
- If a .env is committed, rotate the secrets first; history rewrites do not reach existing clones.
.env File Format
A .env file is plain text with one variable per line in KEY=value form; lines starting with # are comments. There is no formal standard, and the loaders disagree on the edge cases, so the safest file is a boring one: no spaces around =, and double quotes around any value containing #, spaces, quotes or line breaks. The test below shows why.
Group related variables together with blank lines and comments for organization. Put database config together, API keys together, and feature flags together. This makes .env files scannable as they grow.
Every value arrives in your program as a string. In JavaScript that means FLAG=false gives the string "false", which is truthy, so if (process.env.FLAG) runs. Compare explicitly (process.env.FLAG === "true") or parse once at startup with a schema.
Worked example: one file, three loaders
On October 12, 2026 we loaded the same nine-line file with the dotenv package (16.6.1), Node.js 22βs built-in --env-file flag and Pythonβs python-dotenv (1.2.2), with HOME_OVERRIDE=fromshell already set in the shell:
| Line in .env | dotenv 16.6.1 | node --env-file | python-dotenv 1.2.2 |
|---|---|---|---|
A = spaced | spaced | spaced | spaced |
B=value # comment | value | value | value |
C="pass#word" | pass#word | pass#word | pass#word |
D=pass#word | pass (cut at #) | pass (cut at #) | pass#word |
export E=exported | exported | exported | exported |
F="line1 / line2" | two lines | two lines | two lines |
G=${FLAG}x | ${FLAG}x (literal) | ${FLAG}x (literal) | falsex (expanded) |
HOME_OVERRIDE=fromfile | fromshell | fromshell | fromshell |
FLAG=false as a boolean | truthy in JS | truthy in JS | truthy in Python (bool("false")) |
Three lessons. An unquoted # ends the value in the Node loaders but not in Python, so a password like pass#word silently becomes pass; quote it. ${VAR} references are expanded by python-dotenv but kept literally by dotenv (which needs the separate dotenv-expand package) and by Node. And none of the three overwrites a variable that is already set in the environment, so a stale shell export beats the file; pass override=True (Python) or override: true (dotenv) if you want the file to win. Our .env Generator does not yet add quotes for you, so quote values with # or spaces by hand.
Variable Naming Conventions
Use SCREAMING_SNAKE_CASE: all uppercase with underscores between words. This is the universal convention across languages and platforms. DATABASE_URL, API_KEY, SMTP_HOST follow this pattern.
Prefix related variables with a common namespace: DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS. This groups variables visually and prevents naming collisions when you have many services.
Avoid generic names like KEY, URL, or SECRET. Always include the service name: STRIPE_API_KEY, SENDGRID_API_KEY, AWS_SECRET_KEY. This prevents confusion when you have multiple similar variables.
Loading .env in Your Language
Node.js: since v20.6.0 Node can read the file itself: node --env-file=.env app.js (multi-line values from v20.12.0, no longer experimental from v22.21.0 and v24.10.0), or call process.loadEnvFile() (v20.12.0+). On older versions, or for options such as override, install the dotenv package and call require('dotenv').config() at the top of your entry file. Either way values appear on process.env.
Python: install python-dotenv (pip install python-dotenv) and call from dotenv import load_dotenv; load_dotenv(). Values appear in os.environ; use os.environ.get('NAME') if a missing variable should not raise a KeyError.
Docker Compose reads a .env file in the project directory automatically, but only to fill in ${VARIABLE} references inside compose.yaml. Those values do not reach the container unless you pass them: list them under environment: or point the service at a file with env_file: .env. Variables set in your shell take precedence over the file. docker compose config --environment shows what Compose resolved.
Security Best Practices
Never commit .env files to version control. Add .env to your .gitignore immediately when creating a new project. This is the single most important .env security rule.
Create a .env.example file with all required variable names but placeholder values: API_KEY=your-api-key-here. Commit this file so teammates know which variables they need to configure.
If a .env file is ever committed, rotate every credential in it first. Deleting the file in a later commit does not remove it from history, and rewriting history (GitHub documents git filter-repo for this) does not help with copies already cloned or forked, so treat the secrets as exposed either way.
Use different .env files for different environments: .env.development, .env.staging, .env.production. Never use production credentials in development β a bug in development code could affect production data.
Frequently Asked Questions
Should I commit .env files to Git?
What if I accidentally committed .env?
Can I use .env in production?
How do I share .env with my team?
- [1]Node.js docs: --env-file (added v20.6.0) β
- [2]Node.js docs: process.loadEnvFile() β
- [3]dotenv (motdotla) README β
- [4]python-dotenv on PyPI β
- [5]Docker docs: Variable interpolation in Compose β
- [6]Docker docs: Set environment variables within a container β
- [7]GitHub docs: Removing sensitive data from a repository β