home / documentation / v19 / path

Path

目录

Added in: v0.10.0

稳定性: 2 - Stable

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

JS

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:

JS

On Windows:

JS

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

On POSIX and Windows:

JS

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

On POSIX and Windows:

JS

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.

M path.basename(path[, suffix])

历史
版本更改
v6.0.0Passing a non-string as the `path` argument will throw now.
v0.1.25Added in: v0.1.25

The path.basename() method returns the last portion of a path, similar to the Unix basename command. Trailing directory separators are ignored.

JS

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:

JS

A TypeError is thrown if path is not a string or if suffix is given and is not a string.

M path.delimiter

Added in: v0.9.3

Provides the platform-specific path delimiter:

  • ; for Windows
  • : for POSIX

For example, on POSIX:

JS

On Windows:

JS

M path.dirname(path)

历史
版本更改
v6.0.0Passing a non-string as the `path` argument will throw now.
v0.1.16Added in: v0.1.16

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.

JS

A TypeError is thrown if path is not a string.

M path.extname(path)

历史
版本更改
v6.0.0Passing a non-string as the `path` argument will throw now.
v0.1.25Added in: v0.1.25

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 there are no . characters other than the first character of the basename of path (see path.basename()) , an empty string is returned.

JS

A TypeError is thrown if path is not a string.

M path.format(pathObject)

历史
版本更改
v19.0.0The dot will be added if it is not specified in `ext`.
v0.11.15Added in: v0.11.15

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.root is ignored if pathObject.dir is provided
  • pathObject.ext and pathObject.name are ignored if pathObject.base exists

For example, on POSIX:

JS

On Windows:

JS

M path.isAbsolute(path)

Added in: v0.11.2

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:

JS

On Windows:

JS

A TypeError is thrown if path is not a string.

M path.join([...paths])

Added in: v0.1.16

The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.

Zero-length path segments are ignored. If the joined path string is a zero-length string then '.' will be returned, representing the current working directory.

JS

A TypeError is thrown if any of the path segments is not a string.

M path.normalize(path)

Added in: v0.1.23

The path.normalize() method normalizes the given path, resolving '..' and '.' segments.

When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on POSIX and \ on Windows). Trailing separators are preserved.

If the path is a zero-length string, '.' is returned, representing the current working directory.

For example, on POSIX:

JS

On Windows:

JS

Since Windows recognizes multiple path separators, both separators will be replaced by instances of the Windows preferred separator (\):

JS

A TypeError is thrown if path is not a string.

M path.parse(path)

Added in: v0.11.15

The path.parse() method returns an object whose properties represent significant elements of the path. Trailing directory separators are ignored, see path.sep.

The returned object will have the following properties:

For example, on POSIX:

JS
TEXT

On Windows:

JS
TEXT

A TypeError is thrown if path is not a string.

M path.posix

历史
版本更改
v15.3.0Exposed as `require('path/posix')`.
v0.11.15Added in: v0.11.15

The path.posix property provides access to POSIX specific implementations of the path methods.

The API is accessible via require('node:path').posix or require('node:path/posix').

M path.relative(from, to)

历史
版本更改
v6.8.0On Windows, the leading slashes for UNC paths are now included in the return value.
v0.5.0Added in: v0.5.0

The path.relative() method returns the relative path from from to to based on the current working directory. If from and to each resolve to the same path (after calling path.resolve() on each), a zero-length string is returned.

If a zero-length string is passed as from or to, the current working directory will be used instead of the zero-length strings.

For example, on POSIX:

JS

On Windows:

JS

A TypeError is thrown if either from or to is not a string.

M path.resolve([...paths])

Added in: v0.3.4

  • ...paths string A sequence of paths or path segments
  • Returns: string

The path.resolve() method resolves a sequence of paths or path segments into an absolute path.

The given sequence of paths is processed from right to left, with each subsequent path prepended until an absolute path is constructed. For instance, given the sequence of path segments: /foo, /bar, baz, calling path.resolve('/foo', '/bar', 'baz') would return /bar/baz because 'baz' is not an absolute path but '/bar' + '/' + 'baz' is.

If, after processing all given path segments, an absolute path has not yet been generated, the current working directory is used.

The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.

Zero-length path segments are ignored.

If no path segments are passed, path.resolve() will return the absolute path of the current working directory.

JS

A TypeError is thrown if any of the arguments is not a string.

M path.sep

Added in: v0.7.9

Provides the platform-specific path segment separator:

  • \ on Windows
  • / on POSIX

For example, on POSIX:

JS

On Windows:

JS

On Windows, both the forward slash (/) and backward slash (\) are accepted as path segment separators; however, the path methods only add backward slashes (\).

M path.toNamespacedPath(path)

Added in: v9.0.0

On Windows systems only, returns an equivalent namespace-prefixed path for the given path. If path is not a string, path will be returned without modifications.

This method is meaningful only on Windows systems. On POSIX systems, the method is non-operational and always returns path without modifications.

M path.win32

历史
版本更改
v15.3.0Exposed as `require('path/win32')`.
v0.11.15Added in: v0.11.15

The path.win32 property provides access to Windows-specific implementations of the path methods.

The API is accessible via require('node:path').win32 or require('node:path/win32').