🟢 Node.js Intermediate

What is environment-based configuration in Node.js?

Answer

Environment-based configuration separates configuration values (database URLs, API keys, feature flags, ports) from code, loading different values per environment (development, testing, staging, production). This follows the Twelve-Factor App methodology. Approaches: (1) Environment variables via process.env — the most portable, supported by all deployment platforms; (2) dotenv — loads .env files into process.env for local development; (3) Configuration modulesconfig npm package supports JSON/YAML/JS config files per environment with inheritance and CLI overrides; (4) Secrets managers — AWS Secrets Manager, HashiCorp Vault, Kubernetes secrets for production credentials (never hardcode in files). Best practices: validate required env vars on startup (fail fast if DB_URL is missing); provide type-safe config by parsing env vars into a config object at startup; use .env.example as documentation; never commit .env or secrets; use different JWT secrets, database URLs, and API keys per environment.