Node.js traps · Chapter 13 of 16
process.exit() before the file is written
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: lose last night's settlement on purpose
Write reproduce() in repro.js. main in settlement-job.js ends the process it runs in, so run it the way the scheduler does, in a node process of its own: make a temporary folder (mkdtempSync in os.tmpdir()), then start a child with spawnSync from node:child_process, with process.execPath, the arguments '--input-type=module', '-e' and a small script that imports main from './settlement-job.js', makes 1,240 rows ({ booking, show, seats, amount }, any values) and awaits main(rows, <a file in your folder>), and the options { cwd: import.meta.dirname, timeout: 5000 }. Once it has ended, count the rows in the file (the lines after the header, or 0 when there is no file), remove the folder and resolve to the count.
The check runs your reproduce() twice: beside the original settlement-job.js it must resolve to 0 (the log said 1240 rows, the file has none), and beside a fixed settlement-job.js to 1240. Never call main in the repro's own process. No real waiting beyond the child itself: reproduce() must finish within 2 seconds.
- Task 2
settlement-job.js: a settlement file that is whole when the job ends
Fix writeSettlement(rows, outPath) and main(rows, outPath) in settlement-job.js. writeSettlement must resolve to the row count only once the file holds the header and every row, in order (with no rows: the header only), and must reject with the stream's error when the file cannot be made (for example in a folder that does not exist). main, run as a script in a node process of its own, must leave a whole file, log settlement: <count> rows written and end with exit code 0.
Keep the exports, the header booking,show,seats,amount, the row format and the log line as they are. The check uses its own rows, reads the file the moment writeSettlement resolves, runs main with 3,000 rows in a separate node process and reads the file after the process ended, and writes into a folder that does not exist.
- Task 3
settlement-job.test.js: a test that would have caught it
Write a vitest test file, settlement-job.test.js, that would have caught the empty file before it reached a venue owner. The check runs your file three times: beside the original settlement-job.js, where at least one test must fail; beside the fixed settlement-job.js, where every test must pass; and beside a wrong fix that ends the stream but does not wait for it to finish, where at least one test must fail.
So call writeSettlement with some rows and a file in a temporary folder, and the moment it resolves (no waiting of your own), read the file and check both sides: it resolved to the row count, and the file holds the header and every row. Reading a missing file throws, so treat that as an empty file. Never call main in the test, as it would end vitest's own process. Each test must finish within 3 seconds.
What you will learn
- repro.js: lose last night's settlement on purpose
- settlement-job.js: a settlement file that is whole when the job ends
- settlement-job.test.js: a test that would have caught it
More in Modules
Build "process.exit() before the file is written" 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.