Small Footprint URL Parser
url-parse is a URL parsing library designed for both Node.js and browser environments, emphasizing a small footprint. Originally created in 2014, it addressed the lack of a standardized WHATWG URL API across platforms. The package is currently at version 1.5.10 and receives infrequent updates, focusing on stability. It provides two distinct API interfaces: one that emulates the Node.js `url` module and another that behaves similarly to the modern browser `URL` interface. Its core differentiator is a pure string parsing approach, introduced in version 1.0.0, which ensures broad compatibility, particularly in environments without DOM access like Web Workers. It also bundles and exposes the `querystringify` module for robust query string handling. Critically, the project maintainers strongly recommend using the native `URL` interface for new development due to its superior security and accuracy, positioning `url-parse` primarily as a solution for legacy compatibility.
Common errors
-
url.query remains a string, not an object
cause The `parser` option for query string parsing is `false` by default, meaning the query string is stored as a raw string.fixPass `true` as the second argument to the `URLParse` constructor/function: `const url = new URLParse('http://example.com?a=1', true);` to enable default querystring parsing. -
TypeError: URLParse is not a constructor (or 'parse is not a function')
cause Incorrect CommonJS or ES Module import syntax. The package primarily exports a callable constructor function as its default export.fixFor CommonJS: `const URLParse = require('url-parse');`. For ES Modules, which might require bundler support for this CJS-first package: `import URLParse from 'url-parse';`. Avoid named imports like `import { URLParse } from 'url-parse';` for the main export. -
Relative URL does not resolve correctly without a base URL
cause When parsing a relative URL string (e.g., '/path' or '../file.html'), `url-parse` requires a `baseURL` argument to resolve it into an absolute URL. Without it, the result might be unexpected or incomplete.fixProvide a base URL string or `URLParse` object as the second argument when parsing relative URLs: `const resolvedUrl = new URLParse('/images/logo.png', 'http://example.com/');`
Warnings
- deprecated The maintainers strongly recommend using the native WHATWG `URL` API (available in Node.js and modern browsers) for new development. They cite better security and accuracy, positioning `url-parse` primarily for legacy compatibility due to inconsistencies that can be weaponized in security attacks.
- gotcha By default, the `parser` option for querystring parsing is `false`. To parse the query string into an object, you must explicitly pass `true` as the second argument to the `URLParse` constructor/function, or a custom parsing function. Otherwise, the `query` property will remain an unparsed string (e.g., '?key=value').
- breaking Version 1.0.0 fundamentally changed the parsing strategy from a RegExp-based solution to a pure string parsing approach. While intended to improve cross-environment compatibility and reduce Firefox-specific issues, this was a significant internal rewrite that could introduce subtle differences in how certain malformed or edge-case URLs are parsed compared to prior versions.
- breaking Version 0.1 moved from a DOM-based parsing solution (leveraging `<a>` elements) to a Regular Expression solution. This change specifically impacted environments without DOM access, such as Web Workers. While later superseded, it represents a significant shift in compatibility and parsing technique for that version.
Install
-
npm install url-parse -
yarn add url-parse -
pnpm add url-parse
Imports
- URLParse (constructor/function)
import { URLParse } from 'url-parse'; const URLParse = require('url-parse').URLParse;import URLParse from 'url-parse'; // Or for CommonJS: const URLParse = require('url-parse'); - querystringify (module)
import { querystringify } from 'url-parse';import querystringify from 'url-parse/lib/querystringify'; // Or for CommonJS: const querystringify = require('url-parse/lib/querystringify');
Quickstart
'use strict';
const URLParse = require('url-parse');
// Basic parsing with 'new' keyword (optional)
const urlObj1 = new URLParse('https://user:pass@github.com:8080/foo/bar?baz=qux#hash');
console.log('Protocol:', urlObj1.protocol); // 'https:'
console.log('Host:', urlObj1.host); // 'github.com:8080'
console.log('Pathname:', urlObj1.pathname); // '/foo/bar'
console.log('Query (unparsed):', urlObj1.query); // '?baz=qux'
// Parsing as a function with querystring parsing enabled
const urlObj2 = URLParse('http://localhost:3000/path?key=value&arr[]=1&arr[]=2', true);
console.log('Protocol:', urlObj2.protocol); // 'http:'
console.log('Query (parsed):', urlObj2.query); // { key: 'value', arr: [ '1', '2' ] }
// Using a base URL for relative paths
const baseUrl = 'http://example.com/a/b/';
const relativeUrlObj = new URLParse('../c', baseUrl);
console.log('Resolved Path:', relativeUrlObj.pathname); // '/a/c'
console.log('Resolved URL:', relativeUrlObj.href); // 'http://example.com/a/c'
// Accessing querystringify directly
const querystringify = require('url-parse/lib/querystringify');
const qString = querystringify.stringify({ name: 'Alice', age: 30 });
console.log('Stringified Query:', qString); // 'name=Alice&age=30'