Path operations and portability
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Combine and inspect path segments
import path from "node:path";const file = path.posix.join("reports", "2026", "../summary.csv");console.log(file);console.log(path.posix.dirname(file), path.posix.basename(file), path.posix.extname(file));Run in this fixture directory:
node main.mjsExpected stdout:
reports/summary.csvreports summary.csv .csvExpected exit status: 0. Expected stderr: empty. Normalization removes the 2026/.. pair; the extension includes its leading dot.
How it works
join() joins segments and normalizes separators and dot segments. resolve() works from right to left until an absolute path is found, falling back to the current working directory. relative(from,to) describes a path between locations. basename, dirname, and extname extract parts. normalize simplifies a string; it does not resolve symlinks or check access. On Windows, drive letters and backslashes have distinct semantics, so use path.win32 for a Windows-style input even on Linux. For trusted fixture names, explicit segments are sufficient; untrusted path containment needs separate design and filesystem-aware checks.
Use platform-specific path rules
import path from "node:path";console.log(path.posix.resolve("/catalog", "/archive", "books.json"));console.log(path.win32.join("C:\\reports", "today.csv"));console.log(path.posix.relative("/catalog", "/catalog/data/books.json"));Run in this fixture directory:
node main.mjsExpected stdout:
/archive/books.jsonC:\reports\today.csvdata/books.jsonExpected exit status: 0. Expected stderr: empty. The absolute archive segment controls resolve; the Windows variant emits backslashes without touching the actual Windows filesystem.
Common mistake
Do not use a normalized path as proof that an input stays inside a trusted directory. Even a lexical prefix check can miss sibling names and symlink escapes. Prefer a fixed mapping of allowed identifiers to known files.
Try it yourself
Predict the difference between posix.join and posix.resolve when the second segment starts with a slash. Then compute the extension and basename of a fictional report.csv.
Further reading: Official documentation 1. Related ByteMind reference candidate: node-path-join (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Path operations and portability — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which method produces an absolute path using the current directory when necessary?
Does normalize verify file containment?
How can Linux code interpret Windows-style paths?