Node.js theorytheory 0/50 · 0%
I/O · easy

10. The path module

Building filesystem paths safely across operating systems.

`path` builds and inspects file paths without hard-coding `/` or `\\`, which differ between POSIX and Windows.

js
import path from "node:path";

const full = path.join(__dirname, "data", "chains.json");
console.log(path.extname(full));  // ".json"
console.log(path.basename(full)); // "chains.json"
console.log(path.resolve("./config")); // absolute path from cwd

`path.join` also normalizes redundant separators and `..` segments, which matters when paths are assembled from multiple config values — using string concatenation instead is a common source of subtle cross-platform bugs.

Check your understanding

  1. 1. Why use path.join instead of string concatenation with '/'?

  2. 2. What does path.extname('chains.json') return?

  3. 3. What does path.resolve produce?