Lesson 11 / 28

The path Module

Build file paths that work on every OS.

Joining paths

path.join uses the right separator for the current OS.

import path from "node:path";

const filePath = path.join("data", "users", "list.json");
console.log(filePath);                // data/users/list.json
console.log(path.basename(filePath)); // list.json
console.log(path.extname(filePath));  // .json

__dirname in ESM

ES modules don't have __dirname — derive it from import.meta.url with fileURLToPath.