pathslash
node:path, platform-correct, with forward-slash output.
pathslash is a tiny wrapper around node:path that always outputs forward slashes (/) while keeping the correct per-platform semantics. It never reimplements path logic. Every function delegates to node:path and only flips the separators in the result.
Why
- Forward slashes everywhere. On Windows, the
win32functions output/instead of\, which is what bundlers, route maps, and URLs expect. - Correct semantics for free. Drive letters, UNC paths, and case-insensitive
relativebehave exactly as innode:path, because they arenode:path. - POSIX-safe. On POSIX, a backslash is a valid filename character, so it is never rewritten there.
- A typed drop-in. Same API and types as
node:path.
Install
npm i pathslash
Usage
The default export and the top-level named exports follow the host platform, exactly like node:path:
import path, { join } from "pathslash";
join("src", "a.ts"); // 'src/a.ts'
path.sep; // '/'
To force a specific platform, use the win32 and posix namespaces. They work on any host OS:
import { win32, posix } from "pathslash";
win32.join("C:\\a", "b"); // 'C:/a/b' (forward slashes)
win32.normalize("C:\\a\\..\\b"); // 'C:/b'
win32.relative("C:/Foo/Bar", "C:/foo/baz"); // '../baz' (case-insensitive)
posix.join("a", "b\\c"); // 'a/b\\c' (backslash kept)
toSlash(path)
Safely normalizes separators to /. On Windows it converts \ to /. On POSIX it returns the path unchanged, because a backslash is a valid filename character there and rewriting it would corrupt the path. Use it for paths that come from outside this package, such as process.cwd():
import { toSlash } from "pathslash";
toSlash("C:\\a\\b"); // 'C:/a/b' on Windows, unchanged on POSIX
Notes
win32.sepis"/"instead of"\\", andwin32.delimiterstays";".\\?\(extended-length) paths stay verbatim.toSlashand everywin32.*function leave them untouched, because a forward slash is a literal character there, not a separator.toNamespacedPathalways returns a native, backslash path, since\\?\paths are only valid with backslashes.matchesGlobmirrorsnode:path. It exists only on Node versions that ship it (22.5+) and isundefinedon older ones.- The types are a drop-in too:
import type { ParsedPath, FormatInputPathObject, PlatformPath } from "pathslash". - Everything else matches
node:path, separators aside.