Environment Variables and .env
Managing configuration via environment variables: process.env, .env files, security boundaries, and Twelve-Factor App principles.
Environment Variables and .env
Environment variables (env vars) are dynamic key-value pairs stored in the operating system process memory. They allow developers to configure applications across different environments (local development, staging, production) without hardcoding secrets or URLs into source code.
1. Setting and Reading Variables in the Terminal
Temporary (Current Command Only)
PORT=8080 node server.jsSession-Wide (Exported)
export DATABASE_URL="postgresql://user:pass@localhost:5432/mydb"
echo $DATABASE_URL2. Using .env Files
In modern frameworks (Next.js, Vite, Node.js), environment variables are stored in .env files in the project root:
# .env.local (DO NOT COMMIT TO GIT)
DATABASE_URL="postgresql://postgres:secret@localhost:5432/app"
JWT_SECRET="super-secret-key-32-chars-long"
# Public variables exposed to the frontend browser:
NEXT_PUBLIC_API_URL="https://api.example.com"Git Security Rule
Always add .env, .env.local, and .env.production to your .gitignore:
.env
.env*.localCommit a .env.example template containing variable names with dummy placeholder values so team members know what variables are required.
3. Reading Environment Variables in Code
Node.js / Next.js Server Components:
const dbUrl = process.env.DATABASE_URL;
const isProd = process.env.NODE_ENV === "production";
if (!dbUrl) {
throw new Error("DATABASE_URL environment variable is missing!");
}Schema Validation with Zod (Best Practice):
import { z } from "zod";
const envSchema = z.object({
DATABASE_URL: z.string().url(),
PORT: z.coerce.number().default(3000),
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
});
export const env = envSchema.parse(process.env);4. Common Mistakes
- Accidentally Committing Secrets to Public Git Repositories: If you accidentally push API keys or database passwords to GitHub, assume they are compromised immediately and rotate the keys.
- Expecting
process.envin Static Client Bundles: Standard server environment variables are stripped during frontend compilation. Only variables explicitly prefixed (e.g.NEXT_PUBLIC_orVITE_) are bundled into client JavaScript.
Related Topics
Shell Fundamentals & Scripting
Mastering shell interpreters: bash vs. zsh, configuration files (.zshrc, .bashrc), PATH variable, aliases, and shell scripting basics.
Operating System Processes and Signals
Understanding OS processes: PIDs, memory boundaries, background daemons, exit codes, and Unix signals (SIGINT, SIGTERM, SIGKILL).