Skip to main content

Path module reference

The path module provides utilities for working with file and directory paths and can be accessed using an import:

import { join } from "path";`

const grpcOperationPath = join("../", "gRPC", "my-operation");

Variables

delimiter

const delimiter: ";" | ":";

Provides the platform-specific path delimiter.


sep

const sep: "\" | "/";

Provides the platform-specific path segment separator.

Functions

basename()

Call Signature

function basename(path: string): string;

Returns the last portion of a path, similar to the Unix basename command. Trailing directory separators are ignored.

Parameters
ParameterTypeDescription
pathstring
Returns

string

Call Signature

function basename(path: string, suffix: string): string;

Returns the last portion of a path, similar to the Unix basename command. Trailing directory separators are ignored.

Parameters
ParameterTypeDescription
pathstring
suffixstring
Returns

string


dirname()

function dirname(path: string): string;

Returns the directory name of a path, similar to the Unix dirname command. Trailing directory separators are ignored.

Parameters

ParameterTypeDescription
pathstring

Returns

string


extname()

function extname(path: string): string;

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, an empty string is returned.

Parameters

ParameterTypeDescription
pathstring

Returns

string


format()

function format(pathObject: FormatInputPathObject): string;

Formats a file path from a PathObject.

Parameters

ParameterTypeDescription
pathObjectFormatInputPathObjectThe PathObject containing the path components.

Returns

string

A formatted file path string.


isAbsolute()

function isAbsolute(path: string): boolean;

Determines if the literal path is absolute. Therefore, it’s not safe for mitigating path traversals. If the given path is a zero-length string, false will be returned.

Parameters

ParameterTypeDescription
pathstringpath to test.

Returns

boolean


join()

function join(...paths: string[]): string;

Join all arguments together and normalize the resulting path.

Parameters

ParameterTypeDescription
...pathsstring[]paths to join.

Returns

string


normalize()

function normalize(path: string): string;

Normalize a string path, reducing '..' and '.' parts. When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used.

Parameters

ParameterTypeDescription
pathstringstring path to normalize

Returns

string


parse()

function parse(path: string): ParsedPath;

See ParsedPath.
Parses a file path into its constituent components.

Parameters

ParameterTypeDescription
pathstringThe file path to parse.

Returns

ParsedPath

A ParsedPath record containing the parsed components.


relative()

function relative(from: string, to: string): string;

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.

Parameters

ParameterTypeDescription
fromstringThe source path.
tostringThe target path.

Returns

string


resolve()

function resolve(...paths: string[]): string;

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 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.

Parameters

ParameterTypeDescription
...pathsstring[]A sequence of paths or path segments

Returns

string

Type Aliases

FormatInputPathObject

type FormatInputPathObject = {
base: string;
dir: string;
ext: string;
name: string;
root: string;
};

Represents the parsed components of a file path.

Properties

base?
optional base: string;

The base name of the file (including the extension).

dir?
optional dir: string;

The directory path from the root to the base name.

ext?
optional ext: string;

The file extension (including the leading period). If there is no extension, it will be an empty string.

name?
optional name: string;

The file name without the extension.

root?
optional root: string;

The root of the path such as '/' or 'c:'.


ParsedPath

type ParsedPath = {
base: string;
dir: string;
ext: string;
name: string;
root: string;
};

Represents the parsed components of a file path.

Properties

base
base: string;

The base name of the file (including the extension).

dir
dir: string;

The directory path from the root to the base name.

ext
ext: string;

The file extension (including the leading period). If there is no extension, it will be an empty string.

name
name: string;

The file name without the extension.

root
root: string;

The root of the path such as '/' or 'c:'.