~2 min read • Updated Dec 30, 2025
1. Windows vs POSIX
The default behavior of path depends on the operating system. For consistent results:
path.win32: Windows-specific methods.path.posix: POSIX-specific methods.
2. Basic Methods
path.basename(path[, suffix]): Returns the last portion of a path (file name).path.dirname(path): Returns the directory name.path.extname(path): Returns the file extension.
path.basename('/foo/bar/file.txt'); // 'file.txt'
path.dirname('/foo/bar/file.txt'); // '/foo/bar'
path.extname('index.html'); // '.html'
3. Constructing and Parsing Paths
path.format(pathObject): Builds a path string from an object.path.parse(path): Breaks a path into root, dir, base, name, and ext.
path.parse('/home/user/file.txt');
// { root: '/', dir: '/home/user', base: 'file.txt', name: 'file', ext: '.txt' }
4. Joining and Normalizing
path.join([...paths]): Joins path segments.path.normalize(path): Normalizes a path (resolves .. and .).path.resolve([...paths]): Resolves segments into an absolute path.
path.join('/foo', 'bar', 'baz'); // '/foo/bar/baz'
path.normalize('/foo/bar//baz/..'); // '/foo/bar'
path.resolve('www', 'static/img'); // '/home/user/www/static/img'
5. Path Checks
path.isAbsolute(path): Checks if a path is absolute.path.relative(from, to): Returns the relative path between two paths.
path.isAbsolute('/foo/bar'); // true
path.relative('/data/test', '/data/impl'); // '../impl'
6. OS-Specific Constants
path.delimiter: Path delimiter in environment variables (: on POSIX, ; on Windows).path.sep: Path segment separator (/ on POSIX, \ on Windows).
7. Windows-Specific Features
path.toNamespacedPath(path): Converts to a namespace-prefixed path (Windows only).path.win32: Windows-specific implementation of path methods.
Conclusion
The path module in Node.js provides powerful tools for managing file and directory paths across different operating systems. With its methods, developers can parse, construct, normalize, and validate paths, ensuring cross-platform compatibility in applications.
Written & researched by Dr. Shahin Siami