How Node really works · Chapter 9 of 34

Files with fs

By the end of this chapter you can save, read, add to, inspect and delete files and folders, and treat a missing file as an answer rather than a crash. 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

Every night Ravi counts the stock (litres of milk, kilos of sugar, packets of Parle-G) into a notebook. Meera wants the counts in files, so the app can warn him before the milk runs out on a monsoon morning. Kabir also wants a sales log that grows all day, and a scratch folder that is wiped at closing time.

So far everything your code keeps is gone when the process stops. Files outlive it, but bring new problems: a folder that does not exist yet, a log that must keep its earlier lines, and a read that must not freeze the order server.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    writeStock: save the day's stock counts, one file per item

    Write writeStock(dir, counts). counts is an object such as { milk: 12, sugar: 5 }. For each item it writes the file <dir>/stock/<item>.txt holding the count as text and nothing else (12, with no newline). It returns a promise that resolves when every file is written.

    dir and its parents may not exist yet: create every missing folder. On a later run the folder is already there; old counts are overwritten, and files for items not in counts are left as they are.

  2. Task 2

    readStock: read the stock folder back into an object

    Write readStock(dir). It reads every file in <dir>/stock whose extension is exactly .txt and returns a promise of { item: count }, the item being the file name without .txt and the count a number (a file may end in a newline: 12 and a newline gives 12).

    Other files in the folder, such as notes.md or milk.txt.bak, are ignored. When <dir>/stock does not exist at all, it resolves to {}.

  3. Task 3

    logSale: add each sale to the end of the sales log

    Write logSale(dir, line). It adds the line and a newline to the end of <dir>/sales.log, creating the file the first time, and resolves when it is done.

    Earlier lines stay: after three calls the file holds three lines, each ending in a newline, in the order they were logged.

  4. Task 4

    fileInfo: a file's size and type, or null when it is missing

    Write fileInfo(filePath). It resolves to { size, isFolder, modified }: size in bytes, isFolder true for a folder and false for a file, and modified the time of the last change, as a Date.

    When nothing is at filePath, it resolves to null instead of rejecting. Any other error still rejects.

  5. Task 5

    clearScratch: clear the scratch folder at closing time

    Write clearScratch(dir). It deletes the folder <dir>/scratch with everything inside it, folders included, and resolves to true. When there is no scratch folder, it resolves to false.

    Nothing else in dir is touched, and calling it twice in a row resolves true, then false.

  6. Task 6 · interview question

    Interview: predict when a sync read and an async read finish

    The starter shows a snippet that pushes start, starts an async read with a then that pushes async read done, does a sync read of the same file, then pushes sync read done and end, and waits for the async read. Set predictedOrder to the four entries, as the same strings, in the order they are pushed.

    The check runs the same snippet and compares the two lists in order.

What you will learn

  • node:fs/promises: reading, writing, appending and making folders, with await.
  • readdir, stat and rm: what is in a folder, what one entry is, and deleting.
  • Error codes: telling "not there" from a real failure.
  • Sync and async calls: why the server uses the promise versions.

More in Files and data

Build "Files with fs" 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.