How Node really works · Chapter 8 of 34

The path module

By the end of this chapter you can build, take apart, tidy and compare file paths, and keep a file name from a customer inside its folder. 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

Ravi's scripts glue paths together with + and '/'. When the data folder in the config ends in a slash he gets //, when it is relative the files land wherever the script was started, and splitting v2.final.json on the dot loses half its name. Anjali's new "download your receipt" link is worse: it takes a file name from the customer, and one sneaky name can read any file on the server.

A path looks like a string, but it has rules: separators, . and .., absolute and relative. String methods know none of them. Node's path module knows all of them.

What you build: 6 tasks

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

  1. Task 1

    describeFile: split a stock file path into folder, name and extension

    Write describeFile(filePath). It returns { folder, name, ext }: folder is the folder part ('' when the path has none), ext is the extension with its dot ('' when there is none), and name is the file name without that extension.

    Only the part after the last dot is the extension, so 2026-10-04.backup.csv has the name 2026-10-04.backup. A dotfile such as .env has no extension: its name is .env and its ext is ''.

  2. Task 2

    changeExtension: swap a report's extension without touching its folder

    Write changeExtension(filePath, newExt). It returns the same path, with the same folder and name, but with newExt in place of the old extension. newExt may come with or without its dot ('json' or '.json'), and the result always has exactly one dot before it.

    Only the last extension changes (2026-10-04.backup.csv becomes 2026-10-04.backup.txt), a path with no extension gets one (/srv/tapri/menu becomes /srv/tapri/menu.json), and a bare file name stays bare (sales.csv becomes sales.json, with no ./ in front).

  3. Task 3

    receiptPath: the absolute path of a day's receipt

    Write receiptPath(root, date, orderId). date is YYYY-MM-DD. It returns the absolute path <root>/receipts/<year>/<month>/r-<orderId>.txt, with the year and month taken from the date as they are (the month keeps its leading zero).

    root may end in a slash or hold . and .. pieces, and the result is tidy all the same. A relative root, such as data, is taken from the current working folder.

  4. Task 4

    cleanPath: tidy up a messy path from a config file

    Write cleanPath(messy). It returns the same path with . pieces and double slashes removed and each .. folded into the folder before it, and with no slash at the end. The root path / stays /.

    A .. with nothing before it to fold stays (a/../../b gives ../b), and a path that folds away completely gives . (stock/.. gives .).

  5. Task 5

    linkBetween: link the daily summary to a receipt in another folder

    Write linkBetween(fromFile, toFile). Both are absolute paths of files. It returns the relative path a link inside fromFile needs to reach toFile, that is, the way from the folder fromFile is in to toFile.

    Two files in the same folder give just the file name (logo.png).

  6. Task 6 · interview question

    Interview: stop a file name from escaping the receipts folder

    Write safeJoin(root, name). name comes from a customer. It returns the absolute path of name inside root (a relative root is taken from the current working folder), tidied, such as /srv/tapri/receipts/2026/10/r-507.txt. A name with .. that stays inside, such as old/../r-1.txt, is fine.

    It throws an Error when the result is outside root or is root itself: ../../etc/passwd, an absolute name such as /etc/passwd, .., . and a sibling folder whose name starts the same way (../receipts-private/keys.txt) all throw. A file whose name only starts with two dots, such as ..backup.txt, is inside and allowed.

What you will learn

  • join and resolve: building a path from pieces, relative or absolute.
  • parse, format and friends: taking a path apart and putting it back together.
  • normalize and relative: tidying a path, and the way from one place to another.
  • Path traversal: why a joined path can leave its folder, and how to tell.

More in Files and data

Build The path module 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.