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 modules — config 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.
Previous
What is HTTP status codes and which ones are most important in Node.js APIs?
Next
How does Node.js handle concurrency without multiple threads?