LogoPear Docs

bare-path

Path manipulation library for JavaScript

stable

bare-path — Path manipulation library for JavaScript. It is a native addon.

Mirrors the Node.js path module.

npm i bare-path

Usage

const path = require('bare-path')

path.join('foo', 'bar') // foo/bar on posix, foo\bar on windows

API

Joining and resolving

path.join(...paths: string[]): string

Source

Joins all given path segments using the platform-specific separator, then normalizes the result.

Parameters

ParameterTypeDefaultDescription
pathsstring[]The path segments to join together, in the order given.

Throws

  • TypeError — thrown if any path segment is not a string.

path.resolve(...args: string[]): string

Source

Resolves a sequence of paths or path segments into an absolute path.

Parameters

ParameterTypeDefaultDescription
argsstring[]The path segments to resolve, processed from right to left until an absolute path is constructed.

Returns string — Falls back to the current working directory (or that of the resolved drive, on Windows) if none of the given segments is absolute.

Throws

  • TypeError — thrown if any path segment is not a string.

path.normalize(path: string): string

Source

Normalizes path, resolving . and .. segments.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to normalize.

Throws

  • TypeError — thrown if path is not a string.

path.relative(from: string, to: string): string

Source

Returns the relative path from from to to.

Parameters

ParameterTypeDefaultDescription
fromstringThe path to resolve the relative path from.
tostringThe path to resolve the relative path to.

Returns string — Both from and to are resolved to absolute paths before comparing, and an empty string is returned if they resolve to the same path.

Throws

  • TypeError — thrown if from or to is not a string.

Inspecting paths

path.basename(path: string, suffix?: string): string

Source

Returns the last portion of path, similar to the Unix basename command.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to extract the last portion from.
suffix?stringAn optional suffix to remove from the end of the result, if it matches exactly.

Returns string — An empty string if suffix equals path exactly.

Throws

  • TypeError — thrown if path, or a defined suffix, is not a string.

path.dirname(path: string): string

Source

Returns the directory name of path, similar to the Unix dirname command.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to extract the directory name from.

Throws

  • TypeError — thrown if path is not a string.

path.extname(path: string): string

Source

Returns the extension of path, from the last . to the end of the string.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to extract the extension from.

Returns string — An empty string if path has no . in its last segment, or if that segment's only . is its first character (e.g. a dotfile).

Throws

  • TypeError — thrown if path is not a string.

path.isAbsolute(path: string): boolean

Source

Returns true if path is an absolute path.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to test.

Returns boolean — On Windows, a drive-qualified path such as C:\foo is also absolute; on POSIX, only paths starting with / are absolute.

Throws

  • TypeError — thrown if path is not a string.

path.toNamespacedPath(path: string): string

Source

Converts path to a namespace-prefixed path on Windows; returns path unchanged on POSIX.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to convert.

Returns string — On Windows, resolves path and prefixes UNC paths with \\?\UNC\ and drive-letter paths with \\?\; other paths (including non-string input) are returned unchanged.

Platform variants and separators

path.sep: '/' | '\\'

Source

The platform-specific path segment separator: \ on Windows, / elsewhere.

path.delimiter: ':' | ';'

Source

The platform-specific path delimiter: ; on Windows, : elsewhere.

path.posix

Source

The POSIX-specific implementation of the path methods.

path.win32

Source

The Windows-specific implementation of the path methods.

See also

On this page