Node.js traps · Chapter 12 of 16
Environment variables are strings
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modules
- 3 tasks
- Node.js
- In the all labs plan
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: read staging's settings the way the API did
Write reproduce() in repro.js. Call loadConfig from config.js with staging's settings, written the way the environment hands them over, as strings: { SEND_SMS: 'false', PORT: '8080' }. Resolve to an object with two of the settings it gave back: { sendSms, adminPort }. Pass the settings as an object; do not change process.env.
The check runs your reproduce() twice: beside the original config.js it must resolve to { sendSms: 'false', adminPort: '80801' } (a truthy string, and a port glued on instead of added), and beside a fixed config.js to { sendSms: false, adminPort: 8081 }. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
config.js: settings that mean what they say
Fix loadConfig(env = process.env) in config.js so every setting is read as what it means. PORT and MAX_SEATS are whole numbers: PORT from 1 to 65535 (3000 when unset), MAX_SEATS from 1 to 20 (10 when unset), and adminPort is PORT + 1, a number. SEND_SMS is true for 'true' or '1', and false for 'false', '0', an empty string or no variable at all. An empty string counts as unset for the numbers too. Any other value throws an Error whose message names the variable (SEND_SMS, PORT or MAX_SEATS).
Keep the export, the shape { port, adminPort, sendSms, maxSeats } and the log line config: port <port>, admin port <admin>, sms <on or off> as they are. The check uses its own settings: every spelling above, values like '8080.5', '30s', '0', '70000', 'ten' and 'yes please', and process.env itself when no argument is given.
- Task 3
config.test.js: a test that would have caught it
Write a vitest test file, config.test.js, that would have stopped staging's SMS before they went out. The check runs your file three times: beside the original config.js, where at least one test must fail; beside the fixed config.js, where every test must pass; and beside a wrong fix that reads the numbers right but turns SEND_SMS into Boolean(the string), where at least one test must fail.
So call loadConfig with settings written as strings, the way the environment gives them, and check both sides: 'false' gives sendSms false and 'true' gives true, and PORT '8080' gives port 8080 and adminPort 8081, as numbers. Each test must finish within 3 seconds.
What you will learn
- repro.js: read staging's settings the way the API did
- config.js: settings that mean what they say
- config.test.js: a test that would have caught it
More in Modules
Build "Environment variables are strings" yourself
Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.