node:path/posix
Node-compatible POSIX path manipulation utilities (node:path/posix).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed. Always uses POSIX "/" separators and ":"
delimiters regardless of the host operating system.
import ... from "node:path/posix"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import posix from "node:path/posix";
console.log(posix.join("/etc", "vael", "config.json"));console.log(posix.dirname("/var/log/app.log"));joinv0.0.0-alpha.0resolvev0.0.0-alpha.0normalizev0.0.0-alpha.0. and .. segments.relativev0.0.0-alpha.0from to to.dirnamev0.0.0-alpha.0path using POSIX rules.basenamev0.0.0-alpha.0path, optionally stripping suffix.extnamev0.0.0-alpha.0path.parsev0.0.0-alpha.0path into { root, dir, base, ext, name } using POSIX rules.formatv0.0.0-alpha.0FormatInputPathObject into a POSIX path string.isAbsolutev0.0.0-alpha.0true if path is an absolute POSIX path.matchesGlobv0.0.0-alpha.2true if path matches the glob pattern using POSIX rules.toNamespacedPathv0.0.0-alpha.2path unchanged using POSIX rules.Functions
Section titled “Functions”export function join(...paths: string[]): string;
Join path segments and normalize the result using POSIX rules.
| Name | Type | Description |
|---|---|---|
...paths | string[] | Path segments to join. |
string — Normalized POSIX path string.TypeError— when any segment is not a string.
import { join } from "node:path/posix";
console.log(join("/usr", "local", "bin")); // "/usr/local/bin"resolve
Section titled “resolve”export function resolve(...paths: string[]): string;
Resolve a sequence of paths into an absolute POSIX path.
| Name | Type | Description |
|---|---|---|
...paths | string[] | Sequence of path segments processed from right to left. |
string — Resolved absolute POSIX path string.TypeError— when any segment is not a string.
normalize
Section titled “normalize”export function normalize(path: string): string;
Normalize a POSIX path, resolving . and .. segments.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to normalize. |
string — Normalized POSIX path string.TypeError— whenpathis not a string.
relative
Section titled “relative”export function relative(from: string, to: string): string;
Solve the relative POSIX path from from to to.
| Name | Type | Description |
|---|---|---|
from | string | Origin POSIX path. |
to | string | Destination POSIX path. |
string — Relative POSIX path string from from to to.TypeError— whenfromortois not a string.
dirname
Section titled “dirname”export function dirname(path: string): string;
Return the directory name of path using POSIX rules.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to inspect. |
string — Parent directory portion of path.TypeError— whenpathis not a string.
basename
Section titled “basename”export function basename(path: string, suffix?: string): string;
Return the last portion of path, optionally stripping suffix.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to inspect. |
suffix optional | string | Optional trailing suffix to strip when matched. |
string — Final path component.TypeError— whenpathorsuffix(when provided) is not a string.
extname
Section titled “extname”export function extname(path: string): string;
Return the extension of path.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to inspect. |
string — Extension including leading dot, or "" if none.TypeError— whenpathis not a string.
export function parse(path: string): ParsedPath;
Parse path into { root, dir, base, ext, name } using POSIX rules.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to parse. |
ParsedPath — Parsed path components.TypeError— whenpathis not a string.
format
Section titled “format”export function format(pathObject: FormatInputPathObject): string;
Format a FormatInputPathObject into a POSIX path string.
| Name | Type | Description |
|---|---|---|
pathObject | FormatInputPathObject | Object specifying path components. |
string — Formatted POSIX path string.TypeError— whenpathObjectis not an object or any component is not a string.
isAbsolute
Section titled “isAbsolute”export function isAbsolute(path: string): boolean;
Return true if path is an absolute POSIX path.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to test. |
boolean — true if path begins with "/", otherwise false.TypeError— whenpathis not a string.
matchesGlob
Section titled “matchesGlob”export function matchesGlob(path: string, pattern: string): boolean;
Return true if path matches the glob pattern using POSIX rules.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string to test. |
pattern | string | Glob pattern string. |
boolean — true if path matches pattern, otherwise false.TypeError— whenpathorpatternis not a string.
toNamespacedPath
Section titled “toNamespacedPath”export function toNamespacedPath(path: string): string;
Return path unchanged using POSIX rules.
| Name | Type | Description |
|---|---|---|
path | string | POSIX path string. |
string — path unchanged.TypeError— whenpathis not a string.
Interfaces
Section titled “Interfaces”ParsedPath
Section titled “ParsedPath”export interface ParsedPath {
root: string;
dir: string;
base: string;
ext: string;
name: string;
}
Parsed components returned by parse(path).
| Property | Type | Description | Since |
|---|---|---|---|
root | string | Root of the POSIX path ("/" for absolute paths, "" for relative). | v0.0.0-alpha.0 |
dir | string | Directory portion of the path without trailing slash (except "/"). | v0.0.0-alpha.0 |
base | string | Final path component including file extension (for example "file.txt"). | v0.0.0-alpha.0 |
ext | string | File extension including leading dot (for example ".txt"), or "". | v0.0.0-alpha.0 |
name | string | Final path component without its extension (for example "file"). | v0.0.0-alpha.0 |
FormatInputPathObject
Section titled “FormatInputPathObject”export interface FormatInputPathObject {
root?: string;
dir?: string;
base?: string;
ext?: string;
name?: string;
}
Input object accepted by format(pathObject).
| Property | Type | Description | Since |
|---|---|---|---|
root optional | string | Root prefix used when dir is omitted. | v0.0.0-alpha.0 |
dir optional | string | Directory portion (takes precedence over root when provided). | v0.0.0-alpha.0 |
base optional | string | Complete filename (takes precedence over name and ext when provided). | v0.0.0-alpha.0 |
ext optional | string | File extension used when base is omitted. | v0.0.0-alpha.0 |
name optional | string | Base filename without extension used when base is omitted. | v0.0.0-alpha.0 |
PlatformPath
Section titled “PlatformPath”export interface PlatformPath {
readonly sep: string;
readonly delimiter: string;
join(...paths: string[]): string;
resolve(...paths: string[]): string;
normalize(path: string): string;
relative(from: string, to: string): string;
dirname(path: string): string;
basename(path: string, suffix?: string): string;
extname(path: string): string;
parse(path: string): ParsedPath;
format(pathObject: FormatInputPathObject): string;
isAbsolute(path: string): boolean;
matchesGlob(path: string, pattern: string): boolean;
toNamespacedPath(path: string): string;
readonly posix: PlatformPath;
readonly win32: PlatformPath;
}
Platform-specific path manipulation interface (node:path, posix, win32).
| Property | Type | Description | Since |
|---|---|---|---|
sep readonly | string | Platform-specific path segment separator ("/" for POSIX). | v0.0.0-alpha.0 |
delimiter readonly | string | Platform-specific PATH environment variable delimiter (":" for POSIX). | v0.0.0-alpha.0 |
posix readonly | PlatformPath | POSIX-specific implementation of PlatformPath. | v0.0.0-alpha.0 |
win32 readonly | PlatformPath | Windows-specific implementation of PlatformPath. | v0.0.0-alpha.0 |
PlatformPath.join
Section titled “PlatformPath.join”join(...paths: string[]): string;
Join all given path segments together and normalize the resulting path.
| Name | Type | Description |
|---|---|---|
...paths | string[] | Path segments to join. |
string — Normalized joined path string.TypeError— when any segment is not a string.
PlatformPath.resolve
Section titled “PlatformPath.resolve”resolve(...paths: string[]): string;
Resolve a sequence of paths or path segments into an absolute path.
| Name | Type | Description |
|---|---|---|
...paths | string[] | Sequence of path segments processed from right to left. |
string — Resolved absolute path string.TypeError— when any segment is not a string.
PlatformPath.normalize
Section titled “PlatformPath.normalize”normalize(path: string): string;
Normalize path, resolving "." and ".." segments.
| Name | Type | Description |
|---|---|---|
path | string | Path string to normalize. |
string — Normalized path string.TypeError— whenpathis not a string.
PlatformPath.relative
Section titled “PlatformPath.relative”relative(from: string, to: string): string;
Return the relative path from from to to.
| Name | Type | Description |
|---|---|---|
from | string | Origin path. |
to | string | Destination path. |
string — Relative path string from from to to.TypeError— whenfromortois not a string.
PlatformPath.dirname
Section titled “PlatformPath.dirname”dirname(path: string): string;
Return the directory name of path.
| Name | Type | Description |
|---|---|---|
path | string | Path string to inspect. |
string — Parent directory portion of path.TypeError— whenpathis not a string.
PlatformPath.basename
Section titled “PlatformPath.basename”basename(path: string, suffix?: string): string;
Return the last portion of path, optionally stripping a trailing suffix.
| Name | Type | Description |
|---|---|---|
path | string | Path string to inspect. |
suffix optional | string | Optional trailing extension or suffix to remove. |
string — Final path component.TypeError— whenpathorsuffix(when provided) is not a string.
PlatformPath.extname
Section titled “PlatformPath.extname”extname(path: string): string;
Return the extension of path, from the last "." in the basename to end of string.
| Name | Type | Description |
|---|---|---|
path | string | Path string to inspect. |
string — Extension string including leading dot, or "" if none.TypeError— whenpathis not a string.
PlatformPath.parse
Section titled “PlatformPath.parse”parse(path: string): ParsedPath;
Parse path into { root, dir, base, ext, name }.
| Name | Type | Description |
|---|---|---|
path | string | Path string to parse. |
ParsedPath — Object describing the constituent components of path.TypeError— whenpathis not a string.
PlatformPath.format
Section titled “PlatformPath.format”format(pathObject: FormatInputPathObject): string;
Format a FormatInputPathObject into a path string.
| Name | Type | Description |
|---|---|---|
pathObject | FormatInputPathObject | Object specifying path components. |
string — Formatted path string.TypeError— whenpathObjectis not an object or any present component is not a string.
PlatformPath.isAbsolute
Section titled “PlatformPath.isAbsolute”isAbsolute(path: string): boolean;
Return true if path is an absolute path.
| Name | Type | Description |
|---|---|---|
path | string | Path string to test. |
boolean — true if path is absolute, otherwise false.TypeError— whenpathis not a string.
PlatformPath.matchesGlob
Section titled “PlatformPath.matchesGlob”matchesGlob(path: string, pattern: string): boolean;
Return true if path matches the glob pattern using POSIX rules.
| Name | Type | Description |
|---|---|---|
path | string | Path string to test. |
pattern | string | Glob pattern string. |
boolean — true if path matches pattern, otherwise false.TypeError— whenpathorpatternis not a string.
PlatformPath.toNamespacedPath
Section titled “PlatformPath.toNamespacedPath”toNamespacedPath(path: string): string;
Return path unchanged on POSIX.
| Name | Type | Description |
|---|---|---|
path | string | Path string to convert. |
string — path unchanged.TypeError— whenpathis not a string.
Constants
Section titled “Constants”export const sep: "/";
POSIX path segment separator ("/").
delimiter
Section titled “delimiter”export const delimiter: ":";
POSIX path list delimiter (":").
export const posix: PlatformPath;
POSIX path utilities.
export const win32: PlatformPath;
Windows path utilities.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: PlatformPath;
export default _default;
Default export object providing the POSIX PlatformPath API.
