Node.js path module providing utilities for working with file and directory paths in a cross-platform manner
npx @tessl/cli install tessl/npm-path@0.12.0Path is an exact copy of Node.js's built-in path module published as a standalone npm package. It provides comprehensive utilities for working with file and directory paths in a cross-platform manner, handling platform-specific differences between Windows and POSIX systems including path separators, drive letters, and UNC paths.
npm install pathconst path = require("path");ES6 imports:
import * as path from "path";Individual function imports:
const { resolve, join, dirname, basename, extname } = require("path");TypeScript imports (when using @types/node):
import * as path from "path";
import { resolve, join, dirname, basename, extname } from "path";const path = require("path");
// Join paths
const fullPath = path.join("/users", "john", "documents", "file.txt");
// Result: "/users/john/documents/file.txt" (on POSIX)
// Result: "\\users\\john\\documents\\file.txt" (on Windows)
// Resolve absolute path
const absolutePath = path.resolve("../docs", "readme.md");
// Result: absolute path based on current working directory
// Extract path components
const filePath = "/home/user/project/index.js";
console.log(path.dirname(filePath)); // "/home/user/project"
console.log(path.basename(filePath)); // "index.js"
console.log(path.extname(filePath)); // ".js"
// Parse path into components
const parsed = path.parse(filePath);
// Result: { root: "/", dir: "/home/user/project", base: "index.js", ext: ".js", name: "index" }The path module provides platform-aware path utilities through a conditional export system:
process.platformpath.win32 and path.posix are always available regardless of platformCore path resolution functionality for converting relative paths to absolute paths and normalizing path structures.
function resolve(...paths: string[]): string;
function normalize(path: string): string;
function isAbsolute(path: string): boolean;Essential path manipulation operations for joining paths and computing relative paths between locations.
function join(...paths: string[]): string;
function relative(from: string, to: string): string;Path parsing utilities for extracting components from file paths and reconstructing paths from components.
function dirname(path: string): string;
function basename(path: string, ext?: string): string;
function extname(path: string): string;
function parse(pathString: string): ParsedPath;
function format(pathObject: PathObject): string;Platform-specific path operations and constants for Windows and POSIX systems.
function _makeLong(path: string): string;
const sep: string;
const delimiter: string;// Access platform-specific implementations
const posix: typeof path;
const win32: typeof path;Access both Windows and POSIX implementations regardless of current platform:
const path = require("path");
// Force POSIX behavior on any platform
const posixPath = path.posix.join("/usr", "local", "bin");
// Always results in: "/usr/local/bin"
// Force Windows behavior on any platform
const windowsPath = path.win32.join("C:", "Users", "John");
// Always results in: "C:\\Users\\John"interface ParsedPath {
/** Root portion of path (e.g., '/', 'C:\\') */
root: string;
/** Directory portion of path */
dir: string;
/** File name including extension */
base: string;
/** File extension including leading dot */
ext: string;
/** File name without extension */
name: string;
}
interface PathObject {
/** Root portion of path */
root?: string;
/** Directory portion of path */
dir?: string;
/** File name including extension */
base?: string;
}