Web Development • Published August 14, 2026

Mastering URL Parsing in JavaScript and Node.js: The Complete Developer Guide

Complete guide to parsing URLs in modern JavaScript and Node.js. Master WHATWG URL API, URLSearchParams, query string handling, and relative URL resolution.

Learn how to parse, validate, and manipulate URLs in modern JavaScript and Node.js using the standard WHATWG URL and URLSearchParams APIs.

Frequently Asked Questions

Q1. Why was Node.js url.parse() deprecated?

The legacy url.parse() method used regex-based parsing that diverged from browser implementations and RFC specifications, creating security vulnerabilities like Server-Side Request Forgery (SSRF) and hostname spoofing.

Q2. How do I safely check if a string is a valid URL in JavaScript?

In modern browsers and Node.js v19.9.0+, you can use URL.canParse(urlString). For broader compatibility, wrap new URL(urlString) inside a try...catch block.

Q3. How do I extract query parameters from a URL in JavaScript?

Instantiate a URL object with const parsed = new URL(urlStr) and access parsed.searchParams.get("paramName") or parsed.searchParams.getAll("paramName") for array parameters.

Q4. Can the WHATWG URL class parse relative URLs?

Yes, but you must pass a valid base URL as the second argument: new URL("/api/users", "https://example.com"). Passing a relative URL alone will throw a TypeError.