To parse a URL in Node.js, create an instance of the global URL class with new URL(input, base). It splits the string into parts such as protocol, hostname, pathname, search and hash, and it throws a TypeError if the string is not a valid URL. This is the standard way to do a Node.js URL parse today, and it replaces the legacy url.parse() function.
The URL class follows the WHATWG URL Standard, the same one browsers use, so code you write in Node.js behaves the same way in the browser. It is available as a global since Node.js 10, and you can also import it from the node:url module.
Pass an absolute URL string to the constructor and read its properties.
Every property is a string. port is an empty string when the URL uses the default port of its scheme (443 for https:, 80 for http:), so do not expect a number. origin is read-only, while the other parts can be assigned to, and href updates to match.
| Property | Example value | Notes |
|---|---|---|
protocol | https: | Includes the trailing colon |
host | shop.example.com:8443 | Hostname plus port when the port is not the default |
hostname | shop.example.com | No port |
port | 8443 | Empty string for default ports |
pathname | /products/shoes | Always starts with / for special schemes |
search | ?color=red&size=42 | Includes the leading ?, empty string if none |
hash | #reviews | Includes the leading #, empty string if none |
searchParams | URLSearchParams object | Live view of the query string |
Strings without a scheme, or with broken parts, make the constructor throw. Wrap user-supplied input in a try and catch block, or use the static method URL.canParse() (added in Node.js 19.9 and 18.17) to check first.
A relative string such as /just/a/path is invalid on its own, because there is nothing to resolve it against. That is what the second constructor argument is for.
The second argument is a base URL. The constructor resolves the first argument against it, the same way a browser resolves a link in a page.
Note that the trailing slash in the base matters. With the base https://docs.example.com/guide/intro (no slash), new URL("setup", base) replaces the last segment and gives https://docs.example.com/guide/setup.
The searchParams property is a URLSearchParams object linked to the URL. Changing it rewrites url.search, and the reverse is also true.
get() returns null when the key is missing, and getAll() returns every value for a repeated key. Values are decoded for you (+ and %20 become a space), and encoded again when you write them back. You can also build a query string from an object with new URLSearchParams({ a: "1", b: "2" }).toString().
You do not have to concatenate strings. Create a URL from a base and set the parts you need, so the encoding is always correct.
The ampersand and the non-ASCII letter are percent-encoded, so the query string cannot be broken by user input. Hostnames with non-ASCII letters are converted to Punycode automatically.
ES modules in Node.js expose import.meta.url, which is a file: URL. Use the helpers from node:url to convert between URLs and file system paths instead of slicing strings.
These functions handle percent-encoding and Windows drive letters correctly, which manual string handling usually gets wrong.
Older code uses url.parse(string), which returns a plain object with fields such as query and pathname. It is deprecated in the Node.js documentation, it does not follow the WHATWG standard, and it has had security problems with how it reads hostnames. For new code, use new URL(). If you meet url.parse(str, true) in an old codebase, replace it with new URL(str, base) and use searchParams instead of the parsed query object.
No. URL and URLSearchParams are globals. You can still write import { URL } from "node:url" if you prefer explicit imports, and the class is the same.
A bare path has no scheme or host, so it is not an absolute URL. Pass a base as the second argument, for example new URL("/about", "https://example.com").
Use Object.fromEntries(url.searchParams). Repeated keys keep only the last value, so use getAll() when a key can appear more than once.
Yes. JSON.stringify(url) calls toJSON() and returns the href string.
Use new URL(input, base) to parse URLs in Node.js. Read the parts from properties such as hostname, pathname and hash, work with the query string through searchParams, and guard untrusted input with try and catch or URL.canParse(). Use fileURLToPath() and pathToFileURL() for file paths, and avoid the deprecated url.parse() in new code.