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.

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

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.