Cross-platform filepath manipulation helpers
The path module wraps Go's path/filepath package for host-platform path manipulation. It is aliased as both path and node:path. The separator and behavior match the operating system on which the runtime runs.
const path = require("path");
const full = path.join("/tmp", "demo", "file.txt");
// "/tmp/demo/file.txt"
const dir = path.dirname("/tmp/demo/file.txt");
// "/tmp/demo"
const base = path.basename("/tmp/demo/file.txt");
// "file.txt"
const ext = path.extname("/tmp/demo/file.txt");
// ".txt"
const abs = path.isAbsolute("/tmp/demo");
// true
const rel = path.relative("/tmp/demo", "/tmp/demo/output");
// "output"
const resolved = path.resolve("demo", ".."); // resolves against cwd
join(...parts)Joins path elements into a single path using the OS-specific separator. Cleans up .. and . segments.
resolve(...parts)Joins path elements and converts the result to an absolute path relative to the current working directory.
dirname(path)Returns the directory portion of path, excluding the final separator and file name.
basename(path)Returns the last element of path.
extname(path)Returns the file extension, including the leading dot. Returns an empty string when there is no extension.
isAbsolute(path)Returns true if path is absolute on the current platform.
relative(from, to)Returns the shortest relative path from from to to.
path.separatorstring — the OS path separator (/ on Unix, \ on Windows).
path.delimiterstring — the OS path list delimiter (: on Unix, ; on Windows).
| Problem | Cause | Solution |
|---|---|---|
| Paths contain backslashes on Linux | The script assumes Windows separators | Use path.join instead of manual string concatenation |
resolve() returns unexpected root | No arguments provided | Call with explicit starting paths, or accept that it resolves against the process cwd |