Path#

Stability: 2 - Stable

The node:path module provides utilities for working with file and directory paths. It can be accessed using:

const path = require('node:path');
import path from 'node:path';
javascript

Windows vs. POSIX#

The default operation of the node:path module varies based on the operating system on which a Node.js application is running. Specifically, when running on a Windows operating system, the node:path module will assume that Windows-style paths are being used.

So using path.basename() might yield different results on POSIX and Windows:

On POSIX:

path.basename('C:\\temp\\myfile.html');
// Returns: 'C:\\temp\\myfile.html'
js

On Windows:

path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
js

To achieve consistent results when working with Windows file paths on any operating system, use path.win32:

On POSIX and Windows:

path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
js

To achieve consistent results when working with POSIX file paths on any operating system, use path.posix:

On POSIX and Windows:

path.posix.basename('/tmp/myfile.html')