- Assertion Testing
- Buffer
- C/C++ Addons
- Child Processes
- Cluster
- Command Line Options
- Console
- Crypto
- Debugger
- Deprecated APIs
- DNS
- Domain
- Errors
- Events
- File System
- Globals
- HTTP
- HTTPS
- Modules
- Net
- OS
- Path
- Process
- Punycode
- Query Strings
- Readline
- REPL
- Stream
- String Decoder
- Timers
- TLS/SSL
- Tracing
- TTY
- UDP/Datagram
- URL
- Utilities
- V8
- VM
- ZLIB
Node.js v7.10.1 Documentation
Table of Contents
Path#
Stability: 2 - Stable
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.
For example, using the path.basename() function with the Windows file path
C:\temp\myfile.html, will yield different results when running on POSIX than
when run on 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'
path.basename(path[, ext])#
The path.basename() methods returns the last portion of a path, similar to
the Unix basename command. Trailing directory separators are ignored, see
path.sep.
For example:
path.basename('/foo/bar/baz/asdf/quux.html')
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// Returns: 'quux'
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)#
The path.dirname() method returns the directory name of a path, similar to
the Unix dirname command. Trailing directory separators are ignored, see
path.sep.
For example:
path.dirname('/foo/bar/baz/asdf/quux')
// Returns: '/foo/bar/baz/asdf'
A TypeError is thrown if path is not a string.
path.extname(path)#
The path.extname() method returns the extension of the path, from the last
occurrence of the . (period) character to end of string in the last portion of
the path. If there is no . in the last portion of the path, or if the
first character of the basename of path (see path.basename()) is ., then
an empty string is returned.
For example:
path.extname('index.html')
// Returns: '.html'
path.extname('index.coffee.md')
// Returns: '.md'
path.extname('index.')
// Returns: '.'
path.extname('index')
// Returns: ''
path.extname('.index')
// Returns: ''
A TypeError is thrown if path is not a string.
path.format(pathObject)#
The path.format() method returns a path string from an object. This is the
opposite of path.parse().
When providing properties to the pathObject remember that there are
combinations where one property has priority over another:
pathObject.rootis ignored ifpathObject.diris providedpathObject.extandpathObject.nameare ignored ifpathObject.baseexists
For example, on POSIX:
// If `dir`, `root` and `base` are provided,
// `${dir}${path.sep}${base}`
// will be returned. `root` is ignored.
path.format({
root: '/ignored',
dir: '/home/user/dir',
base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt'
// `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the
// platform separator will not be included. `ext` will be ignored.
path.format({
root: '/',
base: 'file.txt',
ext: 'ignored'
});
// Returns: '/file.txt'
// `name` + `ext` will be used if `base` is not specified.
path.format({
root: '/',
name: 'file',
ext: '.txt'
});
// Returns: '/file.txt'
On Windows:
path.format({
dir: 'C:\\path\\dir',
base: 'file.txt'
});
// Returns: 'C:\\path\\dir\\file.txt'
path.isAbsolute(path)#
The path.isAbsolute() method determines if path is an absolute path.
If the given path is a zero-length string, false will be returned.
For example on POSIX:
path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..') // true
path.isAbsolute('qux/') // false
path.isAbsolute('.') // false
On Windows:
path.isAbsolute('//server') // true
path.isAbsolute('\\\\server') // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('C:\\foo\\..') // true
path.isAbsolute('bar\\baz') // false
path.isAbsolute('bar/baz') // false
path.isAbsolute('.') // false