Node.js traps · Chapter 10 of 16
Paths relative to the working directory
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: start the job from another folder on purpose
Write reproduce() in repro.js. Remember the current folder (process.cwd()), move the process into another one with process.chdir (os.tmpdir() from node:os, like the scheduler starting the job from /), call renderSettlement from settlement-mail.js for { name: 'Kala Talkies', tickets: 412, amount: 61840 }, and put the old folder back with process.chdir in a finally, whatever happened. Resolve to the error's code (err.code) when it rejected, and to the mail's first line when it worked.
The check runs your reproduce() twice: beside the original settlement-mail.js it must resolve to 'ENOENT' (no template in that folder), and beside a fixed settlement-mail.js to '<h1>Settlement for Kala Talkies</h1>'. Started from the case's own folder the original works, so the repro must change folder itself, and it must leave the process where it found it. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
settlement-mail.js: find the template wherever the job starts
Fix renderSettlement(venue) in settlement-mail.js so it reads settlement-template.html from next to settlement-mail.js, whatever folder the process was started in. The mail stays as it is: the template without its <!-- --> note lines, with {{venue}}, {{tickets}} and {{amount}} filled in (the amount with Indian commas, 2345600 as 23,45,600).
Keep the export and the template file as they are, and read the template from the file each time. The check renders its own venue with the process in the case's folder, in the temporary folder and in /, puts a copy of your module beside a template of its own and expects that template's text, and runs your module in plain node started from /, where an ES module has no __dirname (vitest gives it one, so code that uses it passes in vitest and crashes there).
- Task 3
settlement-mail.test.js: a test that would have caught it
Write a vitest test file, settlement-mail.test.js, that would have caught the failure before the job moved to the scheduler. The check runs your file three times: beside the original settlement-mail.js, where at least one test must fail; beside the fixed settlement-mail.js, where every test must pass; and beside a wrong fix that builds the path with path.resolve, which still starts from the working folder, where at least one test must fail.
So move into another folder (process.chdir to os.tmpdir()), render a venue's mail, put the old folder back in a finally, and check both sides: the mail came back, and it has the venue's name, the tickets and the amount with Indian commas. Each test must finish within 3 seconds.
What you will learn
- repro.js: start the job from another folder on purpose
- settlement-mail.js: find the template wherever the job starts
- settlement-mail.test.js: a test that would have caught it
More in Modules
Build "Paths relative to the working directory" 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.