Environment configuration — a way to store secrets and settings (API URLs, keys) separately for dev, staging, and production.
Why it matters: Hardcoding API URLs or secrets directly in source code is a security risk and makes deployment painful. Environment variables let you change behavior per deployment target without modifying code. A single codebase can connect to a local dev server, a staging API, and a production database just by changing the .env file.
In Create React App / Next.js:
1# .env (common variables)2REACT_APP_API_URL=https://api.myapp.com3REACT_APP_TITLE=My App45# .env.local (personal, not committed)6REACT_APP_API_KEY=secret-key78# .env.development9REACT_APP_API_URL=http://localhost:30011011# .env.production12REACT_APP_API_URL=https://api.myapp.com
Usage in code:
1const API_URL = process.env.REACT_APP_API_URL;2const isDev = process.env.NODE_ENV === "development";34fetch(`${API_URL}/users`);
In Next.js (App Router):
1# .env.local2DATABASE_URL=postgresql://...34# .env.development.local5DATABASE_URL=postgresql://localhost:5432/mydb
1// Only for server components2const dbUrl = process.env.DATABASE_URL;34// For client components (only NEXT_PUBLIC_)5const apiUrl = process.env.NEXT_PUBLIC_API_URL;
How environment variable priority works:
.env — base configuration, checked into git..env.local — personal overrides, NOT checked into git..env.development / .env.production — environment-specific..env.development.local — personal + environment-specific (highest priority).More specific files override less specific ones.
Common mistakes:
NEXT_PUBLIC_ prefix for secrets (they get bundled into client JS)..env files..env.local to .gitignore (leaks secrets).Important rules:
NEXT_PUBLIC_ or REACT_APP_ — only for client-side variables..env.local to .gitignore.