How Node really works · Chapter 10 of 34
More with files
By the end of this chapter you can walk a folder tree, replace a file so no reader sees half of it, copy without overwriting, read just part of a file, and keep two updates from trampling each other. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Files and data
- 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
A month of receipts later, Tapri's data folder is a tree, and Ravi wants to know how much space it takes. Anjali wants last month's report copied into an archive, without ever overwriting an older copy. On Saturday the app showed an empty menu for a moment, because it read the menu file while the admin page was writing it. And the order counter sometimes loses orders when two come in together.
The tools from chapter 9 fall short here: readdir sees one level, writeFile empties a file before it fills it, readFile loads a whole log to show its last line, and two awaited updates can interleave.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
walk: list every file under the receipts folder, however deep
Write walk(dir). It returns a promise of every file under dir, at any depth, as paths relative to dir with / between the parts (receipts/2026/10/r-507.txt), sorted A to Z.
Folders are not in the list, not even empty ones: a folder that holds only empty folders gives [].
- Task 2
folderSize: add up how much space a folder takes
Write folderSize(dir). It returns a promise of { files, bytes }: how many files there are under dir at any depth, and the sum of their sizes in bytes. Folders do not count as files.
A tree with no files at all gives { files: 0, bytes: 0 }.
- Task 3
writeAtomic: write the menu so readers never see half a file
Write writeAtomic(file, data). It replaces the contents of file with data so that a reader sees the old contents or the new, never part of either: write data to a temp file in the same folder, then rename the temp file over file. It resolves when file holds the new data.
Each call needs a temp name of its own, because the check runs five writeAtomic calls on the same file at once. No temp file may be left behind, even when the write fails: if file is a folder, the call rejects and the folder holds nothing new.
- Task 4
copyReport: copy a report into the archive without overwriting
Write copyReport(src, destDir). It copies the file src into destDir under the same file name, creating destDir and any missing parents, and resolves with the new file's path (destDir joined with the file name).
If a file of that name is already in destDir, it rejects with an error whose code is EEXIST, and the file already there stays exactly as it was.
- Task 5
readTail: read only the end of a long kitchen log
Write readTail(file, bytes). It returns a promise of the last bytes bytes of the file, decoded as UTF-8, read through a file handle so the rest of the file is never loaded. When the file is shorter than bytes, it returns the whole file and nothing more (no extra characters after it).
Close the handle whatever happens.
- Task 6 · interview question
Interview: keep concurrent updates from losing orders
Write createStore(file), a tiny JSON database in one file. It returns { get, update }. get() resolves with the object stored in the file, or {} when the file does not exist yet. update(fn) calls fn with the current object (fn may be async), saves what fn returns with an atomic write, and resolves with that new object.
Updates asked for together must run one at a time, in the order they were asked for, so none is lost: twenty updates that each add one end at 20, and the tenth resolves with a count of 10. If fn throws, that update rejects, nothing is saved for it, and the updates after it still run. No temp file is left in the folder.
What you will learn
- Dirent entries: walking a tree without a stat per name.
- Atomic replacement: a temp file and a rename.
- copyFile flags and file handles: refusing to overwrite, and reading at a position.
- Lost updates: why async code races on one thread, and the cure.
More in Files and data
Build "More with files" 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.