How Node really works · Chapter 6 of 34
Environment and config
By the end of this chapter you can read settings from the environment and a .env file safely, layer them in the right order, refuse to start without the ones that matter, and log them without a single secret. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: How Node runs
- 6 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
On Kabir's laptop the order server runs on port 3000 with a local database and a fake UPI key. On the real server it needs port 8080, the production database and the real UPI secret: same code, different settings. Then Anjali forwards a screenshot from the logging dashboard: the startup line printed the whole config, UPI secret and database password included, where half the team can see it. Meera wants it fixed today.
Reading process.env straight into the code goes wrong quietly. Every value is a string, so 'false' is truthy and '0' turned into a number is easy to lose; a missing database URL shows up as a crash an hour later; and printing the config prints the secrets with it.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
envNumber: a number from the environment, zero included
Write envNumber(env, key, fallback). It returns env[key] as a number: '8080' gives 8080, '2.5' gives 2.5, and '0' gives 0, not the fallback. A missing value (undefined) or an empty string gives fallback.
Any other value that is not a finite number, including one made only of spaces, throws an Error whose message contains the key, for example MAX_RIDERS must be a number, got "lots".
- Task 2
envFlag: an on or off switch from the environment
Write envFlag(env, key, fallback), where fallback is false when it is left out. 'true', '1', 'yes' and 'on' give true, and 'false', '0', 'no' and 'off' give false, in any letter case ('TRUE', 'Yes' and 'False' count).
A missing value or an empty string gives fallback. Any other value, such as 'maybe', throws an Error whose message contains the key.
- Task 3
parseDotenv: a .env parser of your own
Write parseDotenv(text). It returns an object of strings, one entry per KEY=value line, and {} for an empty text. Lines may end in \n or \r\n. Blank lines, lines starting with # (after trimming) and lines with no = are skipped. An export before the key is ignored. Split on the first = and trim both the key and the value, so ' PORT = 9090 ' gives PORT: '9090'.
A value in double or single quotes is exactly what is inside the quotes, # included, and spaces after the closing quote are dropped. An unquoted value ends where whitespace followed by # starts a comment, but a # with no whitespace before it stays (https://tapri.app/menu#today keeps #today). KEY= gives an empty string, and a later line for the same key wins.
- Task 4
loadConfig: defaults, then the .env file, then the environment
Write loadConfig(dir, env), an async function where env is process.env when it is left out. It reads the .env file in the folder dir with your parseDotenv; a missing file counts as empty. For every key in DEFAULTS and REQUIRED, the value comes from env if it is set there, else from the file, else from DEFAULTS.
It resolves to an object with only those keys (a key with no value anywhere is left out, and other keys from env or the file, such as PATH or SHOP_NAME, never appear). Values stay strings.
- Task 5
loadConfig: refuse to start without the required settings, naming all of them
After layering, if any REQUIRED key is missing or an empty string, loadConfig rejects with one Error whose message names every missing key, for example Missing required config: DATABASE_URL, UPI_SECRET.
Keys that are present must not be named: with DATABASE_URL in the file and UPI_SECRET set to an empty string in env, the message names UPI_SECRET only.
- Task 6 · interview question
Interview: fix describeConfig, the startup log that leaks the UPI secret
describeConfig(config) returns every key as KEY=value, one per line joined with \n, sorted by key with the default sort (capitals before small letters). Today it prints secrets. Fix it so that a key whose name contains SECRET, PASSWORD, TOKEN or KEY, in any letter case, shows **, and a value that is a URL with a password shows that password as with the rest of the URL as it was (postgres://tapri:**@db.local:5432/orders).
Every other value, including a URL with no password, is printed exactly as it is.
What you will learn
- Environment values are strings: turn them into numbers and switches, and refuse nonsense.
- The .env file: its format, line by line.
- Layers and required keys: defaults, then the file, then the environment, and no start without the essentials.
- Secrets in logs: secrets hide in names and inside URLs.
More in How Node runs
Build "Environment and config" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.