posix Path Namespace Cross Platform
The path.posix namespace applies forward-slash rules on every OS, keeping join, normalize, and relative output identical across contributor machines.
Code
import path from 'node:path';
// path.posix applies the same separators and ordering on every platform,
// keeping tooling output consistent across contributors' machines
console.log(path.posix.join('/srv/app', '..', 'logs', 'app.log'));
console.log(path.posix.normalize('/srv//app/./logs/'));
// relative() walks up to the common ancestor
console.log(path.posix.relative('/srv/data', '/srv/data/reports/2026'));
console.log(path.posix.relative('/srv/data', '/etc/nginx/nginx.conf'));
console.log(path.posix.basename('/srv/app/config.json', '.json'));
// The platform-specific path object prints backslashes on Windows
console.log(path.join('/srv/app', '..', 'logs', 'app.log'));Output
Captured from a sandboxed run of this snippet in the build pipeline.
/srv/logs/app.log
/srv/app/logs/
reports/2026
../../etc/nginx/nginx.conf
config
\srv\logs\app.logCaveats
- The only import,
node:path, is part of the Node.js standard library.
What each call returns here
Running this file prints five stable strings. The join call resolves to /srv/logs/app.log because .. cancels app; normalize collapses the doubled slash and the dot segment into /srv/app/logs/. The first relative result is just reports/2026, since both inputs sit under the /srv/data ancestor; the second has to climb to the filesystem root first, so it comes back as ../../etc/nginx/nginx.conf. basename strips both the directory and the .json suffix, leaving config. The closing path.join line is the contrast: on Windows the platform-specific object prints backslash separators, which is exactly the drift path.posix prevents across contributor machines and CI runners.
Interactive Tools for This Topic
FAQ
- Are there third-party dependencies?
- No. The imports resolve to Node.js core modules, which the runtime provides.
Report a problem with this exampleOpens a pre-filled issue on GitHub.