Skip to content
VaelJS Blog

vael:path

Lexical POSIX and Windows path manipulation utilities.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Provides host-dialect top-level helpers as well as explicit posix and win32 dialects without touching the filesystem (except resolve, relative, and Windows toNamespacedPath when reading the working directory).

import ... from "vael:path"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-path (default) in vaeljs-lite-core
import { basename, dirname, extname, join, posix } from "vael:path";
const full = join("src", "app", "main.ts");
console.log(dirname(full), basename(full), extname(full));
console.log(posix.join("/etc", "nginx", "..", "hosts")); // "/etc/hosts"
API Symbols
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function join(...paths: string[]): string;

Join and normalize path segments using the build host’s path dialect.

Parameters
NameTypeDescription
...pathsstring[]Path segments to join.
Returns
string — Normalized joined path string.
Exceptions
  • TypeError — when any segment is not a string or contains NUL.
import { join } from "vael:path";
console.log(join("var", "log", "..", "lib", "app.db"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function normalize(path: string): string;

Normalize . and .. segments and separators using the host dialect.

Parameters
NameTypeDescription
pathstringPath string to normalize.
Returns
string — Normalized path string.
Exceptions
  • TypeError — when path is not a string or contains NUL.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function basename(path: string, suffix?: string): string;

Return the final path component, optionally stripping suffix.

Parameters
NameTypeDescription
pathstringPath string.
suffix optionalstringOptional trailing suffix to strip when matched.
Returns
string — Final path component.
Exceptions
  • TypeError — on non-string arguments or NUL bytes.
import { basename } from "vael:path";
console.log(basename("/srv/app/config.json", ".json")); // "config"
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function dirname(path: string): string;

Return the directory portion of path.

Parameters
NameTypeDescription
pathstringPath string.
Returns
string — Directory portion of path.
Exceptions
  • TypeError — on non-string path or NUL bytes.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function extname(path: string): string;

Return the extension (including leading ".") of the last component, or "".

Parameters
NameTypeDescription
pathstringPath string.
Returns
string — Extension string or "".
Exceptions
  • TypeError — on non-string path or NUL bytes.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function isAbsolute(path: string): boolean;

Return true if path is absolute in the host dialect.

Parameters
NameTypeDescription
pathstringPath string to test.
Returns
boolean — true if path is absolute.
Exceptions
  • TypeError — on non-string path or NUL bytes.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function parse(path: string): ParsedPath;

Split path into { root, dir, base, ext, name }.

Parameters
NameTypeDescription
pathstringPath string to parse.
Returns
ParsedPath — ParsedPath component record.
Exceptions
  • TypeError — on non-string path or NUL bytes.
import { parse } from "vael:path";
console.log(parse("/home/user/notes.md"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function format(pathObject: FormatInputPathObject): string;

Format a { root, dir, base, ext, name } object into a path string.

Parameters
NameTypeDescription
pathObjectFormatInputPathObjectPartial path component object.
Returns
string — Formatted path string.
Exceptions
  • TypeError — on invalid pathObject or NUL bytes.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function common(paths: readonly string[]): string;

Return the shared prefix at component boundaries across paths without normalizing or folding case.

Parameters
NameTypeDescription
pathsreadonly string[]Array of path strings to compare.
Returns
string — Shared component-boundary prefix string.
Exceptions
  • TypeError — on invalid array or path strings.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function resolve(...paths: string[]): string;

Resolve a sequence of paths into an absolute path using the host dialect.

Reads the working directory at most once if inputs lack an absolute base.

Parameters
NameTypeDescription
...pathsstring[]Path segments resolved right-to-left.
Returns
string — Absolute resolved path string.
Exceptions
  • TypeError — on non-string segments or NUL bytes.
  • Error — with code: PathErrorCode if the working directory is unavailable.
import { resolve } from "vael:path";
console.log(resolve("examples", "webapp", "main.ts"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function relative(from: string, to: string): string;

Compute the relative path from from to to in the host dialect.

Parameters
NameTypeDescription
fromstringOrigin path.
tostringDestination path.
Returns
string — Relative path string from from to to.
Exceptions
  • TypeError — on non-string arguments or NUL bytes.
  • Error — with code: PathErrorCode if the working directory is unavailable.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function toNamespacedPath(path: string): string;

Convert path to a Windows \\?\ namespaced path on Windows, or return path unchanged on POSIX.

Parameters
NameTypeDescription
pathstringPath string to convert.
Returns
string — Namespaced path on Windows, or path unchanged on POSIX.
Exceptions
  • TypeError — on non-string path or NUL bytes.
  • Error — with code: PathErrorCode if working directory resolution fails.
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface ParsedPath {
  root: string;
  dir: string;
  base: string;
  ext: string;
  name: string;
}

Parsed components returned by parse(path).

Properties
PropertyTypeDescriptionSince
root stringRoot prefix ("/", "C:\\", "\\\\server\\share\\", or "").v0.0.0-alpha.0
dir stringDirectory portion without trailing separator (except root).v0.0.0-alpha.0
base stringFinal path component including extension.v0.0.0-alpha.0
ext stringExtension including leading ".", or "".v0.0.0-alpha.0
name stringFile name without extension.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface FormatInputPathObject {
  root?: string;
  dir?: string;
  base?: string;
  ext?: string;
  name?: string;
}

Input object accepted by format(pathObject).

dir takes precedence over root; base takes precedence over name + ext.

Properties
PropertyTypeDescriptionSince
root optionalstringRoot prefix used when dir is omitted.v0.0.0-alpha.0
dir optionalstringDirectory path (takes precedence over root).v0.0.0-alpha.0
base optionalstringFile base name including extension (takes precedence over name + ext).v0.0.0-alpha.0
ext optionalstringFile extension including leading ".".v0.0.0-alpha.0
name optionalstringFile name without extension.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface PathDialect {
  readonly sep: string;
  readonly delimiter: string;
  join(...paths: string[]): string;
  normalize(path: string): string;
  basename(path: string, suffix?: string): string;
  dirname(path: string): string;
  extname(path: string): string;
  isAbsolute(path: string): boolean;
  parse(path: string): ParsedPath;
  format(pathObject: FormatInputPathObject): string;
  common(paths: readonly string[]): string;
  resolve(...paths: string[]): string;
  relative(from: string, to: string): string;
  toNamespacedPath(path: string): string;
}

Lexical path operations for a fixed syntax dialect (posix or win32).

All arguments must be strings without NUL (\0) characters; performs no filesystem I/O (only resolve, relative, and Windows toNamespacedPath read the working directory when inputs lack an absolute base).

Properties
PropertyTypeDescriptionSince
sep readonlystringPath component separator ("/" for posix, "\\" for win32).v0.0.0-alpha.0
delimiter readonlystringPath list delimiter (":" for posix, ";" for win32).v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
join(...paths: string[]): string;

Join and normalize path segments using this dialect.

Parameters
NameTypeDescription
...pathsstring[]Path segments to join.
Returns
string — Normalized joined path string.
Exceptions
  • TypeError — when any segment is not a string or contains NUL.
methodSince v0.0.0-alpha.0
normalize(path: string): string;

Normalize . and .. segments and separators using this dialect.

Parameters
NameTypeDescription
pathstringPath string to normalize.
Returns
string — Normalized path string.
Exceptions
  • TypeError — when path is not a string or contains NUL.
methodSince v0.0.0-alpha.0
basename(path: string, suffix?: string): string;

Return the final path component, optionally stripping suffix.

Parameters
NameTypeDescription
pathstringPath string.
suffix optionalstringOptional trailing suffix to strip when matched.
Returns
string — Final path component.
Exceptions
  • TypeError — on non-string arguments or NUL bytes.
methodSince v0.0.0-alpha.0
dirname(path: string): string;

Return the directory portion of path.

Parameters
NameTypeDescription
pathstringPath string.
Returns
string — Directory portion of path.
Exceptions
  • TypeError — on non-string path or NUL bytes.
methodSince v0.0.0-alpha.0
extname(path: string): string;

Return the extension (including leading ".") of the last component, or "".

Parameters
NameTypeDescription
pathstringPath string.
Returns
string — Extension string or "".
Exceptions
  • TypeError — on non-string path or NUL bytes.
methodSince v0.0.0-alpha.0
isAbsolute(path: string): boolean;

Return true if path is absolute in this dialect.

Parameters
NameTypeDescription
pathstringPath string to test.
Returns
boolean — true when path is absolute in this dialect.
Exceptions
  • TypeError — on non-string path or NUL bytes.
methodSince v0.0.0-alpha.0
parse(path: string): ParsedPath;

Split path into { root, dir, base, ext, name }.

Parameters
NameTypeDescription
pathstringPath string to split.
Returns
ParsedPath — ParsedPath component record.
Exceptions
  • TypeError — on non-string path or NUL bytes.
methodSince v0.0.0-alpha.0
format(pathObject: FormatInputPathObject): string;

Format a { root, dir, base, ext, name } object into a path string.

Parameters
NameTypeDescription
pathObjectFormatInputPathObjectPartial path component object.
Returns
string — Formatted path string.
Exceptions
  • TypeError — on invalid pathObject or NUL bytes.
methodSince v0.0.0-alpha.0
common(paths: readonly string[]): string;

Return the shared prefix at component boundaries across paths.

Parameters
NameTypeDescription
pathsreadonly string[]Array of path strings to compare.
Returns
string — Common directory prefix string.
Exceptions
  • TypeError — on invalid array or path strings.
methodSince v0.0.0-alpha.0
resolve(...paths: string[]): string;

Resolve a sequence of paths into an absolute path in this dialect.

Parameters
NameTypeDescription
...pathsstring[]Sequence of path segments resolved right-to-left.
Returns
string — Absolute resolved path string.
Exceptions
  • TypeError — on non-string segments or NUL bytes.
  • Error — with code: PathErrorCode if working directory resolution fails.
methodSince v0.0.0-alpha.0
relative(from: string, to: string): string;

Compute the relative path from from to to in this dialect.

Parameters
NameTypeDescription
fromstringOrigin path.
tostringDestination path.
Returns
string — Relative path from from to to.
Exceptions
  • TypeError — on non-string arguments or NUL bytes.
  • Error — with code: PathErrorCode if working directory resolution fails.
methodSince v0.0.0-alpha.0
toNamespacedPath(path: string): string;

Convert path to a Windows \\?\ namespaced path when applicable.

Parameters
NameTypeDescription
pathstringPath string to namespace.
Returns
string — Namespaced path (on win32) or path unchanged (on posix).
Exceptions
  • TypeError — on non-string path or NUL bytes.
  • Error — with code: PathErrorCode if working directory resolution fails.
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type PathErrorCode = "PATH_CWD" | "PATH_CWD_DIALECT" | "PATH_WINDOWS_ROOT";

Error codes thrown by vael:path on PathError.code when resolving against the working directory or invalid Windows roots.

constSince v0.0.0-alpha.0vjs & vjs-lite
export const posix: PathDialect;

Frozen POSIX lexical path dialect.

constSince v0.0.0-alpha.0vjs & vjs-lite
export const win32: PathDialect;

Frozen Windows lexical path dialect.

constSince v0.0.0-alpha.0vjs & vjs-lite
export const SEPARATOR: "/" | "\\";

Host path component separator ("/" on POSIX, "\\" on Windows).

constSince v0.0.0-alpha.0vjs & vjs-lite
export const DELIMITER: ":" | ";";

Host path list delimiter (":" on POSIX, ";" on Windows).

constSince v0.0.0-alpha.0vjs & vjs-lite
export const SEPARATOR_PATTERN: RegExp;

Host path separator pattern (/\/+/ on POSIX, /[\\/]+/ on Windows).