- Assertion testing
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
moduleAPI - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Policies
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Worker threads
- Zlib
Node.js v15.14.0 documentation
Table of contents
- Path
- Windows vs. POSIX
path.basename(path[, ext])path.delimiterpath.dirname(path)path.extname(path)path.format(pathObject)path.isAbsolute(path)path.join([...paths])path.normalize(path)path.parse(path)path.posixpath.relative(from, to)path.resolve([...paths])path.seppath.toNamespacedPath(path)path.win32
Path#
Source Code: lib/path.js
The path module provides utilities for working with file and directory paths.
It can be accessed using:
const path = require('path');
Windows vs. POSIX#
The default operation of the 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 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'
On Windows:
path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
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'
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');
// Returns: 'myfile.html'
On Windows Node.js follows the concept of per-drive working directory.
This behavior can be observed when using a drive path without a backslash. For
example, path.resolve('C:\\') can potentially return a different result than
path.resolve('C:'). For more information, see
this MSDN page.
path.basename(path[, ext])#
The path.basename() method returns the last portion of a path, similar to
the Unix basename command. Trailing directory separators are ignored, see
path.sep.
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
Although Windows usually treats file names, including file extensions, in a
case-insensitive manner, this function does not. For example, C:\\foo.html and
C:\\foo.HTML refer to the same file, but basename treats the extension as a
case-sensitive string:
path.win32.basename('C:\\foo.html', '.html');
// Returns: 'foo'
path.win32.basename('C:\\foo.HTML', '.html');
// Returns: 'foo.HTML'
A TypeError is thrown if path is not a string or if ext is given
and is not a string.
path.delimiter#
Provides the platform-specific path delimiter:
;for Windows:for POSIX
For example, on POSIX:
console.log(process.env.PATH);
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter);
// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
On Windows:
console.log(process.env.PATH);
// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
process.env.PATH.split(path.delimiter);
// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
path.dirname(path)#
path<string>- Returns: