bare-path
Path manipulation library for JavaScript
v3.1.3bare-path — Path manipulation library for JavaScript. It is a native addon.
Mirrors the Node.js path module.
npm i bare-pathUsage
const path = require('bare-path')
path.join('foo', 'bar') // foo/bar on posix, foo\bar on windowsAPI
Joining and resolving
path.join(...paths: string[]): string
Joins all given path segments using the platform-specific separator, then normalizes the result.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
paths | string[] | — | 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
Resolves a sequence of paths or path segments into an absolute path.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
args | string[] | — | 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. Fixed in v3.1.3: a working directory of any length is read. Before, one of 4,096 bytes or more threw an ENOBUFS error.
Throws
TypeError— thrown if any path segment is not a string.
path.normalize(path: string): string
Normalizes path, resolving . and .. segments.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to normalize. |
Throws
TypeError— thrown ifpathis not a string.
path.relative(from: string, to: string): string
Returns the relative path from from to to.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
from | string | — | The path to resolve the relative path from. |
to | string | — | The 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 iffromortois not a string.
Inspecting paths
path.basename(path: string, suffix?: string): string
Returns the last portion of path, similar to the Unix basename command.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to extract the last portion from. |
suffix? | string | — | An 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 ifpath, or a definedsuffix, is not a string.
path.dirname(path: string): string
Returns the directory name of path, similar to the Unix dirname command.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to extract the directory name from. |
Throws
TypeError— thrown ifpathis not a string.
path.extname(path: string): string
Returns the extension of path, from the last . to the end of the string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The 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 (for example a dotfile).
Throws
TypeError— thrown ifpathis not a string.
path.isAbsolute(path: string): boolean
Returns true if path is an absolute path.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The 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 ifpathis not a string.
path.toNamespacedPath(path: string): string
Converts path to a namespace-prefixed path on Windows; returns path unchanged on POSIX.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The 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: '/' | '\\'
The platform-specific path segment separator: \ on Windows, / elsewhere.
path.delimiter: ':' | ';'
The platform-specific path delimiter: ; on Windows, : elsewhere.
path.posix
The POSIX-specific implementation of the path methods.
path.win32
The Windows-specific implementation of the path methods.
Threat model
bare-path is one of the addons Bare compiles into its binary, so every Bare process has it, sealed or not, and it inherits Bare's threat model. The module's own threat model, added in v3.1.3, says where it sits in that one. It manipulates strings and touches no file system: joining a path does not open it. The one thing it reaches outside the process is the working directory, which resolve() reads and which tells sealed code a little about how the host file system is laid out. The risk is in the C that handles paths an attacker chose, where the Windows rules are the fiddly part. Code elsewhere that treats a path as safe because this module normalized it relies on the module agreeing with the OS, so output that differs from what the platform resolves the same input to is reportable.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on