🟢 Node.js Intermediate

What is environment-based configuration in Node.js?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

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.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Node.js experience.