← thecodex.expert · The Codex Family of Knowledge
The Codex / JavaScript / path — Paths

path — Paths

Working with file and directory paths in Node.js — join, resolve, extname, dirname, basename, and cross-platform path handling.

What it is, in plain English: The path module provides utilities for working with file paths. It handles the difference between Unix forward slashes and Windows backslashes automatically. path.join() safely concatenates path segments. path.resolve() converts a relative path to an absolute one. path.extname(), path.basename(), and path.dirname() extract parts of a path. Always use path.join() instead of string concatenation for file paths.
Where this bites you

A Windows path pasted into a normal JavaScript string loses its backslashes, because \ starts an escape sequence. "C:\Users\nina\temp" becomes C:Users, a newline, ina, a tab and emp. \n and \t are real escapes, and \U just drops the backslash. No error is raised. path.join can't help, because the damage is done before it ever sees the string.

import { win32 } from "path";

const folder = "C:\Users\nina\temp";           // copied straight from Explorer
console.log(win32.join(folder, "report.txt"));
console.log(JSON.stringify(folder));           // what the string really contains
// C:Users
// ina	emp\report.txt
// "C:Users\nina\temp"

Working with file paths safely

File paths look like strings — and beginners often treat them as strings. This causes bugs on Windows (which uses backslashes) or when segments are accidentally doubled ("dir//file"). The path module handles all of this.

Import

import { join, resolve, basename, dirname, extname, parse } from "path";

path.join — the most important function

// Always use path.join() to combine path segments:
console.log(join("src", "utils", "helpers.js"));  // "src/utils/helpers.js"
console.log(join("/home", "alice", "docs"));       // "/home/alice/docs"

// Handles extra slashes gracefully:
console.log(join("src/", "/utils/", "file.js"));  // "src/utils/file.js"

// On Windows, path.join uses backslashes automatically:
// join("C:\\Users", "alice", "file.txt")  → C:\Users\alice\file.txt

// ✗ DON'T concatenate paths manually:
const bad  = "src/" + "utils/" + "file.js";   // fragile
const good = join("src", "utils", "file.js"); // safe everywhere

When you have to write a Windows path in source code, double every backslash, or use String.raw, which takes the text exactly as written. Better still, avoid hard-coded absolute paths and build them with join() from pieces such as os.homedir(), which works on every platform:

import { win32 } from "path";

const folder  = "C:\\Users\\nina\\temp";       // every backslash doubled
const folder2 = String.raw`C:\Users\nina\temp`; // or: a raw template string, no escapes
console.log(win32.join(folder, "report.txt"));
console.log(folder === folder2);
// C:\Users\nina\temp\report.txt
// true

path.basename, dirname, extname

const filePath = "/home/alice/projects/app.js";

console.log(basename(filePath));         // "app.js"
console.log(basename(filePath, ".js"));  // "app" (strip extension)
console.log(dirname(filePath));          // "/home/alice/projects"
console.log(extname(filePath));          // ".js"

// Useful patterns:
const ext  = extname(filePath).slice(1);  // "js" (without the dot)
const name = basename(filePath, extname(filePath));  // "app"

path.resolve — relative to absolute

// resolve() works from the current working directory:
// (assuming process.cwd() = "/home/alice/projects")
console.log(resolve("src/app.js"));          // "/home/alice/projects/src/app.js"
console.log(resolve("../other/file.txt"));   // "/home/alice/other/file.txt"
console.log(resolve("/etc/config.json"));    // "/etc/config.json" (absolute, unchanged)

// Chain multiple segments:
console.log(resolve("dist", "assets", "img")); // "/home/alice/projects/dist/assets/img"

Getting __dirname in ES modules

// In CommonJS, __dirname is built-in.
// In ES modules (.mjs / "type":"module"), use import.meta.url:
import { fileURLToPath } from "url";
import { dirname, join }  from "path";

const __filename = fileURLToPath(import.meta.url);  // current file path
const __dirname  = dirname(__filename);              // current directory

// Now use __dirname exactly like you would in CJS:
const configPath = join(__dirname, "config.json");
const distDir    = join(__dirname, "..", "dist");
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.