DeveloperApr 15, 2026 Β· Updated Oct 12, 2026 Β· 5 min read

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.

πŸ”
Try the .env Generator
Free, no signup
β†’
DG
Derek Giordano
Designer & Developer
In this guide
01.env File Format02Worked example: one file, three loaders03Variable Naming Conventions04Loading .env in Your Language05Security Best Practices
⚑ Key Takeaways
  • 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 .envdotenv 16.6.1node --env-filepython-dotenv 1.2.2
A = spacedspacedspacedspaced
B=value # commentvaluevaluevalue
C="pass#word"pass#wordpass#wordpass#word
D=pass#wordpass (cut at #)pass (cut at #)pass#word
export E=exportedexportedexportedexported
F="line1 / line2"two linestwo linestwo lines
G=${FLAG}x${FLAG}x (literal)${FLAG}x (literal)falsex (expanded)
HOME_OVERRIDE=fromfilefromshellfromshellfromshell
FLAG=false as a booleantruthy in JStruthy in JStruthy 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?+
Never. Add .env to .gitignore. Commit a .env.example with placeholder values so teammates know which variables are needed.
What if I accidentally committed .env?+
Rotate every secret in the file first; that is the only step that closes the exposure. Then remove the file, add .env to .gitignore, and if you need it gone from history, rewrite history with git filter-repo as GitHub’s guide describes. Clones and forks made before the rewrite still contain it.
Can I use .env in production?+
Yes, but cloud platforms (AWS, Heroku, Vercel) have built-in secrets management that is more secure. Use .env in development and platform-native config in production.
How do I share .env with my team?+
Use a .env.example file in the repo. Share actual values through a secure channel (password manager, encrypted messaging). Never share secrets via email or Slack.
Try it yourself

Generate .env files for your project β€” free, no signup.

⚑ Open .env Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.