Skip to content
VaelJS Blog

node:url

Node-compatible WHATWG URL, URLSearchParams, and file:// path conversion helpers.

Available in both vjs and vjs-lite runtimes. Enabled by default unless --no-node-compat is passed.

import ... from "node:url"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
import { URL, URLSearchParams, fileURLToPath, pathToFileURL } from "node:url";
const u = new URL("https://example.com/search?q=vaeljs");
u.searchParams.append("mode", "fast");
console.log(u.toString());
const fileUrl = pathToFileURL("/tmp/example.txt");
console.log(fileUrl.href, fileURLToPath(fileUrl));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function fileURLToPath(url: string | URL): string;

Convert a file:// URL string or URL object to a platform filesystem path.

Enforces strict file: scheme, host, NUL-byte, and percent-encoded separator (%2f / %5c) validation (max 64 KiB).

Parameters
NameTypeDescription
urlstring | URLfile:// URL string or URL instance.
Returns
string — Platform-native filesystem path string.
Exceptions
  • TypeError — when url is not a string or URL.
  • Error — with code = "ERR_INVALID_URL" when url is malformed, uses a non-file: scheme, specifies an invalid host, or contains encoded path separators.
  • RangeError — with code = "ERR_OUT_OF_RANGE" when url exceeds 64 KiB.
import { fileURLToPath } from "node:url";
console.log(fileURLToPath("file:///tmp/demo.txt"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function pathToFileURL(path: string): URL;

Convert a filesystem path to a file:// URL object.

Parameters
NameTypeDescription
pathstringFilesystem path string (max 64 KiB).
Returns
URL — URL instance representing the file:// URL for path.
Exceptions
  • TypeError — when path is not a string.
  • Error — with code = "EINVAL" when path is invalid or contains a NUL byte.
  • RangeError — with code = "ERR_OUT_OF_RANGE" when path exceeds 64 KiB.
import { pathToFileURL } from "node:url";
const u = pathToFileURL("/tmp/demo.txt");
console.log(u.href);
classSince v0.0.0-alpha.0vjs & vjs-lite
export class URLSearchParams {
  constructor(init?: string | URLSearchParams | readonly [string, string][] | Iterable<[string, string]> | Record<string, string>);
  readonly size: number;
  append(name: string, value: string): void;
  delete(name: string, value?: string): void;
  get(name: string): string | null;
  getAll(name: string): string[];
  has(name: string, value?: string): boolean;
  set(name: string, value: string): void;
  sort(): void;
  toString(): string;
  forEach(callback: (value: string, name: string, parent: URLSearchParams) => void, thisArg?: unknown): void;
  keys(): IterableIterator<string>;
  values(): IterableIterator<string>;
  entries(): IterableIterator<[string, string]>;
  [Symbol.iterator](): IterableIterator<[string, string]>;
}

Mutable URL query string parameter collection.

Supports application/x-www-form-urlencoded parsing/serialization, iteration, and live synchronization when accessed via URL.searchParams.

import { URLSearchParams } from "node:url";
const params = new URLSearchParams({ tag: "alpha", count: "2" });
params.append("tag", "beta");
console.log(params.getAll("tag")); // ["alpha", "beta"]
console.log(params.toString());
Constructor
constructor(init?: string | URLSearchParams | readonly [string, string][] | Iterable<[string, string]> | Record<string, string>);

Construct a URLSearchParams instance.

Parameters
NameTypeDescription
init optionalstring | URLSearchParams | readonly [string, string][] | Iterable<[string, string]> | Record<string, string>Query string, existing URLSearchParams, array/iterable of [name, value] tuples, or key/value record.
Exceptions
  • TypeError — when a sequence or iterable element is not a [name, value] pair of length 2.
Properties
PropertyTypeDescriptionSince
size readonlynumberTotal number of search parameter entries.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
append(name: string, value: string): void;

Append a specified key/value pair as a new search parameter.

Parameters
NameTypeDescription
namestringParameter name.
valuestringParameter value.
Returns
void
methodSince v0.0.0-alpha.0
delete(name: string, value?: string): void;

Remove search parameters matching name (and optional value).

Parameters
NameTypeDescription
namestringParameter name to remove.
value optionalstringOptional value; when provided, only entries matching both name and value are removed.
Returns
void
methodSince v0.0.0-alpha.0
get(name: string): string | null;

Return the first value associated with name, or null.

Parameters
NameTypeDescription
namestringParameter name to look up.
Returns
string | null — First matching value, or null if not present.
methodSince v0.0.0-alpha.0
getAll(name: string): string[];

Return all values associated with name.

Parameters
NameTypeDescription
namestringParameter name to look up.
Returns
string[] — Array of all matching values in insertion order.
methodSince v0.0.0-alpha.0
has(name: string, value?: string): boolean;

Return true if a parameter matching name (and optional value) exists.

Parameters
NameTypeDescription
namestringParameter name to test.
value optionalstringOptional value to match alongside name.
Returns
boolean — true if a matching parameter exists, otherwise false.
methodSince v0.0.0-alpha.0
set(name: string, value: string): void;

Set the value associated with name to value, removing duplicates.

Parameters
NameTypeDescription
namestringParameter name.
valuestringNew parameter value.
Returns
void
methodSince v0.0.0-alpha.0
sort(): void;

Sort all key/value pairs in place by key code units.

Returns
void
methodSince v0.0.0-alpha.0
toString(): string;

Serialize parameters as a URL query string without leading ?.

Returns
string — Form-urlencoded query string.
methodSince v0.0.0-alpha.0
forEach(callback: (value: string, name: string, parent: URLSearchParams) => void, thisArg?: unknown): void;

Invoke callback for each key/value pair in insertion order.

Parameters
NameTypeDescription
callback(value: string, name: string, parent: URLSearchParams) => voidFunction called with (value, name, parent) for each entry.
thisArg optionalunknownOptional this binding passed to callback.
Returns
void
Exceptions
  • TypeError — when callback is not a function.
methodSince v0.0.0-alpha.0
keys(): IterableIterator<string>;

Iterate over all parameter names.

Returns
IterableIterator<string> — Iterator yielding each parameter name in order.
methodSince v0.0.0-alpha.0
values(): IterableIterator<string>;

Iterate over all parameter values.

Returns
IterableIterator<string> — Iterator yielding each parameter value in order.
methodSince v0.0.0-alpha.0
entries(): IterableIterator<[string, string]>;

Iterate over all [name, value] pairs.

Returns
IterableIterator<[string, string]> — Iterator yielding [name, value] pairs in order.
methodSince v0.0.0-alpha.0
[Symbol.iterator](): IterableIterator<[string, string]>;

Default [Symbol.iterator] yielding [name, value] pairs.

Returns
IterableIterator<[string, string]> — Iterator yielding [name, value] pairs in order.
classSince v0.0.0-alpha.0vjs & vjs-lite
export class URL {
  constructor(url: string | URL, base?: string | URL);
  href: string;
  readonly origin: string;
  protocol: string;
  username: string;
  password: string;
  host: string;
  hostname: string;
  port: string;
  pathname: string;
  search: string;
  readonly searchParams: URLSearchParams;
  hash: string;
  toString(): string;
  toJSON(): string;
  static canParse(url: string | URL, base?: string | URL): boolean;
  static parse(url: string | URL, base?: string | URL): URL | null;
}

WHATWG URL parser and serializer.

Supports inputs up to 64 KiB without embedded NUL bytes.

import { URL } from "node:url";
const url = new URL("/docs/intro", "https://vaeljs.dev");
console.log(url.origin, url.pathname); // "https://vaeljs.dev" "/docs/intro"
Constructor
constructor(url: string | URL, base?: string | URL);

Construct a URL from url and optional base.

Parameters
NameTypeDescription
urlstring | URLAbsolute or relative URL string or URL instance.
base optionalstring | URLOptional base URL used when url is relative.
Exceptions
  • TypeError — with code = "ERR_INVALID_URL" when url or base is invalid or contains NUL, or when called with no arguments.
  • RangeError — with code = "ERR_OUT_OF_RANGE" when url or base exceeds 64 KiB.
Properties
PropertyTypeDescriptionSince
href stringFull serialized URL string; assigning a new URL re-parses the entire URL.v0.0.0-alpha.0
origin readonlystringRead-only ASCII serialization of the URL's origin (scheme://host[:port]).v0.0.0-alpha.0
protocol stringURL scheme including trailing colon (for example "https:").v0.0.0-alpha.0
username stringPercent-encoded username portion of the URL.v0.0.0-alpha.0
password stringPercent-encoded password portion of the URL.v0.0.0-alpha.0
host stringHostname and optional port (for example "example.com:8080").v0.0.0-alpha.0
hostname stringHostname without port (for example "example.com").v0.0.0-alpha.0
port stringDecimal port string, or "" when using the default port or none.v0.0.0-alpha.0
pathname stringPath component starting with "/" when hierarchical (for example "/a/b").v0.0.0-alpha.0
search stringQuery component including leading "?", or "" when empty.v0.0.0-alpha.0
searchParams readonlyURLSearchParamsLive URLSearchParams view synchronized with search.v0.0.0-alpha.0
hash stringFragment identifier including leading "#", or "" when empty.v0.0.0-alpha.0
Methods
methodSince v0.0.0-alpha.0
toString(): string;

Return the serialized URL string (href).

Returns
string — Serialized URL string.
methodSince v0.0.0-alpha.0
toJSON(): string;

Return the serialized URL string for JSON serialization.

Returns
string — Serialized URL string (href).
methodstaticSince v0.0.0-alpha.0
static canParse(url: string | URL, base?: string | URL): boolean;

Return true if url (with optional base) parses as a valid URL.

Parameters
NameTypeDescription
urlstring | URLCandidate URL string or URL instance.
base optionalstring | URLOptional base URL string or URL instance.
Returns
boolean — true if parsing succeeds, otherwise false.
import { URL } from "node:url";
console.log(URL.canParse("https://vaeljs.dev")); // true
console.log(URL.canParse("not a url")); // false
methodstaticSince v0.0.0-alpha.0
static parse(url: string | URL, base?: string | URL): URL | null;

Parse url (with optional base), returning null instead of throwing if invalid.

Parameters
NameTypeDescription
urlstring | URLCandidate URL string or URL instance.
base optionalstring | URLOptional base URL string or URL instance.
Returns
URL | null — Parsed URL instance, or null if invalid.
import { URL } from "node:url";
const maybeUrl = URL.parse("/path", "https://vaeljs.dev");
console.log(maybeUrl?.href);
defaultSince v0.0.0-alpha.0vjs & vjs-lite
const _default: {
  /** Convert a `file://` URL to a platform filesystem path. @since 0.0.0-alpha.0 */
  fileURLToPath: typeof fileURLToPath;
  /** Convert a filesystem path to a `file://` `URL`. @since 0.0.0-alpha.0 */
  pathToFileURL: typeof pathToFileURL;
  /** WHATWG `URL` parser and serializer class. @since 0.0.0-alpha.0 */
  URL: typeof URL;
  /** Mutable URL query string parameter collection class. @since 0.0.0-alpha.0 */
  URLSearchParams: typeof URLSearchParams;
};
export default _default;

Default export object providing { fileURLToPath, pathToFileURL, URL, URLSearchParams }.