Configuration and environment management
Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.
What you will be able to do
#- Separate configuration from code and load it from the environment.
- Define a clear precedence order for layered configuration.
- Validate configuration at startup and fail fast on missing values.
Overview
#The same artifact should run in every environment. Configuration, not code, changes between development, staging and production. The twelve-factor principle is to store config in the environment, not in files committed to the repository.
Environment variables
Read config from process.env or the equivalent, parse it once at startup, and validate it. A process that starts with a missing required value should exit immediately with a clear message rather than fail later under load.
function required(name) {
const value = process.env[name];
if (!value) throw new Error(`Missing required config: ${name}`);
return value;
}
const config = {
port: Number(process.env.PORT || 3000),
databaseUrl: required('DATABASE_URL'),
logLevel: process.env.LOG_LEVEL || 'info',
};
Precedence
When several sources can set a value, define the order explicitly, for example: command-line flags over environment variables over a config file over built-in defaults. Document it, because implicit precedence causes hard-to-find surprises.
Types and parsing
Environment variables are always strings. Coerce numbers and booleans deliberately and reject invalid values. Number('') is 0, and 'false' is a truthy string, so naive parsing is a common bug.
Common pitfalls
- Committing a
.envfile with real values. - Reading config lazily deep in the code, so failures appear far from startup.
- Treating
'false'as a boolean, which is truthy in JavaScript.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this print when FEATURE_ENABLED is set to the string 'false'?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptconst enabled = process.env.FEATURE_ENABLED || false; console.log(enabled ? 'on' : 'off');Exercise 2Implement
Write a startup validator that requires DATABASE_URL, defaults PORT to 3000 as a number, and rejects a PORT that is not a valid integer between 1 and 65535.
starter.javascript// return a validated config object or throwReveal the reference answer
reference.javascriptfunction loadConfig(env = process.env) { const databaseUrl = env.DATABASE_URL; if (!databaseUrl) throw new Error('DATABASE_URL is required'); const port = Number(env.PORT ?? 3000); if (!Number.isInteger(port) || port < 1 || port > 65535) { throw new Error(`Invalid PORT: ${env.PORT}`); } return { databaseUrl, port }; }Authored expected output: A config object with a string databaseUrl and an integer port, or a thrown Error naming the bad value.
Validate once at startup, coerce the port to a number, and reject out-of-range values so the process never starts in a half-configured state.
Exercise 3Debug
A service works in staging but in production connects to the staging database. Config precedence is flags, then environment, then config file, then defaults. Production sets DATABASE_URL in the environment but a baked config file still has staging. What is the bug?
starter.text# production: DATABASE_URL env set; config file has staging URLReveal the reference answer
reference.textThe loader is reading the config file before the environment, or the file overrides the environment. Apply the documented precedence so the environment variable wins, or remove the baked-in value from the image entirely.Authored expected output: Production uses the environment DATABASE_URL, not the value baked into the image.
A config file baked into an image is a fixed input; the environment must be able to override it. The safest fix is to never bake environment-specific values into the artifact.
Common misconceptions
#- Environment variables carry types: they are always strings and must be parsed.
- A .env file is safe to commit: it usually contains secrets and belongs outside version control.
Follow-up practice
#- devops-secrets
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.