environment variablesdotenvconfigurationdevopsdeveloper toolstwelve-factor app

How to Compare .env Files and Manage Environment Variables

Learn how to safely compare .env files between environments, find missing variables, and avoid configuration drift between development and production.

7 min read

Related Tool

Environment Variable Diff

Open tool

Configuration drift is one of the most frustrating sources of bugs. Code works perfectly in development and breaks in production because a required environment variable is missing, has a different value, or uses a different format. The .env file diff tool closes this gap by making differences between environment configurations immediately visible.

How .env Files Work

.env files are plain text files with one KEY=VALUE pair per line. The dotenv library (available for Node.js, Python, Ruby, and many other languages) reads these files at startup and populates environment variables.

DATABASE_URL=postgres://user:password@localhost:5432/myapp
REDIS_URL=redis://localhost:6379
JWT_SECRET=dev-secret-not-for-production
API_KEY=sk_test_abc123
LOG_LEVEL=debug
PORT=3000

Conventions:

  • `.env` — local development values (never commit to git)
  • `.env.example` or `.env.template` — committed to git, shows required variables with placeholder values or documentation
  • `.env.production` — production values, often managed via CI/CD secrets or a secrets manager, never committed
  • `.env.test` — test environment values

Why .env Files Diverge

As a project grows, new configuration values are added for new features. If a developer adds STRIPE_WEBHOOK_SECRET to their local .env and starts using it in code, but forgets to document it in .env.example, other developers and production environments will not have the variable.

This causes cryptic runtime errors: TypeError: Cannot read properties of undefined, Error: Stripe webhook validation failed, or simply silent wrong behavior when code falls back to an undefined value.

Using the DevHexLab .env Diff Tool

Paste two .env files into the left and right panels — for example, your .env.example on the left and your production environment variables on the right. Click Compare.

The diff highlights:

  • Keys present in left but missing in right — variables that production needs but does not have
  • Keys present in right but missing in left — variables in production not documented in the template
  • Keys present in both but with different values — value mismatches (useful for spotting dev vs. prod value differences)

Values flagged as secrets (keys containing SECRET, KEY, PASSWORD, TOKEN) are shown as [hidden] in the diff output to prevent accidental exposure.

.env File Syntax

Comments

# This is a comment
DATABASE_URL=postgres://localhost/myapp

Quoted values

# Quotes are optional but useful for values with spaces
APP_NAME="My Application"
GREETING='Hello, World!'

Multi-line values

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA...
-----END RSA PRIVATE KEY-----"

Variable expansion

BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v1

Best Practices

Commit `.env.example` to git, never `.env`. Add .env to .gitignore. Your .env.example should have every required variable with a descriptive comment and a safe placeholder value.

Validate required variables at startup. Add a startup check that reads the list of required variables and exits with a clear error if any are missing:

const required = ['DATABASE_URL', 'JWT_SECRET', 'STRIPE_API_KEY'];
for (const key of required) {
  if (!process.env[key]) {
    console.error(`Missing required environment variable: ${key}`);
    process.exit(1);
  }
}

Use a secrets manager for production. Do not store production secrets in .env files on servers. Use AWS Secrets Manager, HashiCorp Vault, or your platform's secret management (Vercel environment variables, GitHub Actions secrets, Railway variables).

Review .env.example on every PR. Make .env.example review part of your PR checklist. If a PR adds a new process.env.SOMETHING reference, the example file must be updated.

Conclusion

Environment variable configuration drift is invisible until it breaks something in production. The DevHexLab .env diff tool makes differences between environment configurations explicit and easy to resolve before deployment.