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"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”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));Functions
Section titled “Functions”fileURLToPath
Section titled “fileURLToPath”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).
| Name | Type | Description |
|---|---|---|
url | string | URL | file:// URL string or URL instance. |
string — Platform-native filesystem path string.TypeError— whenurlis not a string orURL.Error— withcode = "ERR_INVALID_URL"whenurlis malformed, uses a non-file:scheme, specifies an invalid host, or contains encoded path separators.RangeError— withcode = "ERR_OUT_OF_RANGE"whenurlexceeds 64 KiB.
import { fileURLToPath } from "node:url";
console.log(fileURLToPath("file:///tmp/demo.txt"));pathToFileURL
Section titled “pathToFileURL”export function pathToFileURL(path: string): URL;
Convert a filesystem path to a file:// URL object.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path string (max 64 KiB). |
TypeError— whenpathis not a string.Error— withcode = "EINVAL"whenpathis invalid or contains a NUL byte.RangeError— withcode = "ERR_OUT_OF_RANGE"whenpathexceeds 64 KiB.
import { pathToFileURL } from "node:url";
const u = pathToFileURL("/tmp/demo.txt");console.log(u.href);Classes
Section titled “Classes”URLSearchParams
Section titled “URLSearchParams”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(init?: string | URLSearchParams | readonly [string, string][] | Iterable<[string, string]> | Record<string, string>);
Construct a URLSearchParams instance.
| Name | Type | Description |
|---|---|---|
init optional | string | URLSearchParams | readonly [string, string][] | Iterable<[string, string]> | Record<string, string> | Query string, existing URLSearchParams, array/iterable of [name, value] tuples, or key/value record. |
TypeError— when a sequence or iterable element is not a[name, value]pair of length 2.
| Property | Type | Description | Since |
|---|---|---|---|
size readonly | number | Total number of search parameter entries. | v0.0.0-alpha.0 |
URLSearchParams.append
Section titled “URLSearchParams.append”append(name: string, value: string): void;
Append a specified key/value pair as a new search parameter.
| Name | Type | Description |
|---|---|---|
name | string | Parameter name. |
value | string | Parameter value. |
voidURLSearchParams.delete
Section titled “URLSearchParams.delete”delete(name: string, value?: string): void;
Remove search parameters matching name (and optional value).
| Name | Type | Description |
|---|---|---|
name | string | Parameter name to remove. |
value optional | string | Optional value; when provided, only entries matching both name and value are removed. |
voidURLSearchParams.get
Section titled “URLSearchParams.get”get(name: string): string | null;
Return the first value associated with name, or null.
| Name | Type | Description |
|---|---|---|
name | string | Parameter name to look up. |
string | null — First matching value, or null if not present.URLSearchParams.getAll
Section titled “URLSearchParams.getAll”getAll(name: string): string[];
Return all values associated with name.
| Name | Type | Description |
|---|---|---|
name | string | Parameter name to look up. |
string[] — Array of all matching values in insertion order.URLSearchParams.has
Section titled “URLSearchParams.has”has(name: string, value?: string): boolean;
Return true if a parameter matching name (and optional value) exists.
| Name | Type | Description |
|---|---|---|
name | string | Parameter name to test. |
value optional | string | Optional value to match alongside name. |
boolean — true if a matching parameter exists, otherwise false.URLSearchParams.set
Section titled “URLSearchParams.set”set(name: string, value: string): void;
Set the value associated with name to value, removing duplicates.
| Name | Type | Description |
|---|---|---|
name | string | Parameter name. |
value | string | New parameter value. |
voidURLSearchParams.sort
Section titled “URLSearchParams.sort”sort(): void;
Sort all key/value pairs in place by key code units.
voidURLSearchParams.toString
Section titled “URLSearchParams.toString”toString(): string;
Serialize parameters as a URL query string without leading ?.
string — Form-urlencoded query string.URLSearchParams.forEach
Section titled “URLSearchParams.forEach”forEach(callback: (value: string, name: string, parent: URLSearchParams) => void, thisArg?: unknown): void;
Invoke callback for each key/value pair in insertion order.
| Name | Type | Description |
|---|---|---|
callback | (value: string, name: string, parent: URLSearchParams) => void | Function called with (value, name, parent) for each entry. |
thisArg optional | unknown | Optional this binding passed to callback. |
voidTypeError— whencallbackis not a function.
URLSearchParams.keys
Section titled “URLSearchParams.keys”keys(): IterableIterator<string>;
Iterate over all parameter names.
IterableIterator<string> — Iterator yielding each parameter name in order.URLSearchParams.values
Section titled “URLSearchParams.values”values(): IterableIterator<string>;
Iterate over all parameter values.
IterableIterator<string> — Iterator yielding each parameter value in order.URLSearchParams.entries
Section titled “URLSearchParams.entries”entries(): IterableIterator<[string, string]>;
Iterate over all [name, value] pairs.
IterableIterator<[string, string]> — Iterator yielding [name, value] pairs in order.URLSearchParams.[Symbol.iterator]
Section titled “URLSearchParams.[Symbol.iterator]”[Symbol.iterator](): IterableIterator<[string, string]>;
Default [Symbol.iterator] yielding [name, value] pairs.
IterableIterator<[string, string]> — Iterator yielding [name, value] pairs in order.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(url: string | URL, base?: string | URL);
Construct a URL from url and optional base.
| Name | Type | Description |
|---|---|---|
url | string | URL | Absolute or relative URL string or URL instance. |
base optional | string | URL | Optional base URL used when url is relative. |
TypeError— withcode = "ERR_INVALID_URL"whenurlorbaseis invalid or contains NUL, or when called with no arguments.RangeError— withcode = "ERR_OUT_OF_RANGE"whenurlorbaseexceeds 64 KiB.
| Property | Type | Description | Since |
|---|---|---|---|
href | string | Full serialized URL string; assigning a new URL re-parses the entire URL. | v0.0.0-alpha.0 |
origin readonly | string | Read-only ASCII serialization of the URL's origin (scheme://host[:port]). | v0.0.0-alpha.0 |
protocol | string | URL scheme including trailing colon (for example "https:"). | v0.0.0-alpha.0 |
username | string | Percent-encoded username portion of the URL. | v0.0.0-alpha.0 |
password | string | Percent-encoded password portion of the URL. | v0.0.0-alpha.0 |
host | string | Hostname and optional port (for example "example.com:8080"). | v0.0.0-alpha.0 |
hostname | string | Hostname without port (for example "example.com"). | v0.0.0-alpha.0 |
port | string | Decimal port string, or "" when using the default port or none. | v0.0.0-alpha.0 |
pathname | string | Path component starting with "/" when hierarchical (for example "/a/b"). | v0.0.0-alpha.0 |
search | string | Query component including leading "?", or "" when empty. | v0.0.0-alpha.0 |
searchParams readonly | URLSearchParams | Live URLSearchParams view synchronized with search. | v0.0.0-alpha.0 |
hash | string | Fragment identifier including leading "#", or "" when empty. | v0.0.0-alpha.0 |
URL.toString
Section titled “URL.toString”toString(): string;
Return the serialized URL string (href).
string — Serialized URL string.URL.toJSON
Section titled “URL.toJSON”toJSON(): string;
Return the serialized URL string for JSON serialization.
string — Serialized URL string (href).URL.canParse
Section titled “URL.canParse”static canParse(url: string | URL, base?: string | URL): boolean;
Return true if url (with optional base) parses as a valid URL.
| Name | Type | Description |
|---|---|---|
url | string | URL | Candidate URL string or URL instance. |
base optional | string | URL | Optional base URL string or URL instance. |
boolean — true if parsing succeeds, otherwise false.import { URL } from "node:url";
console.log(URL.canParse("https://vaeljs.dev")); // trueconsole.log(URL.canParse("not a url")); // falseURL.parse
Section titled “URL.parse”static parse(url: string | URL, base?: string | URL): URL | null;
Parse url (with optional base), returning null instead of throwing if invalid.
| Name | Type | Description |
|---|---|---|
url | string | URL | Candidate URL string or URL instance. |
base optional | string | URL | Optional base URL string or URL instance. |
import { URL } from "node:url";
const maybeUrl = URL.parse("/path", "https://vaeljs.dev");console.log(maybeUrl?.href);Default Export
Section titled “Default Export”default export
Section titled “default export”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 }.
