component-url

raw JSON →
0.2.1 verified Sat Apr 25 auth: no javascript abandoned

A lightweight URL parser using the browser's `<a>` tag for resolution. Version 0.2.1 is the latest stable release; the package appears unmaintained since 2013. Provides parse, isAbsolute, isRelative, isCrossDomain functions. Uses native DOM parsing (no regex) but only works in browsers, not Node.js. Very minimal API compared to modern URL parsers like URL constructor or whatwg-url.

error ReferenceError: window is not defined
cause Running in Node.js where there is no DOM <a> element.
fix
Use Node's built-in url module: const url = require('url'); url.parse('http://...');
error Cannot find module 'url'
cause Not installed via npm; this package is for Component build system only.
fix
Install with 'component install component/url' or use a browser-ready bundler like Duo.
error Uncaught TypeError: url.parse is not a function
cause Using the Node.js url module's parse incorrectly or importing wrong package.
fix
Ensure you are using this specific package (component/url) via Component or copy source.
breaking Only works in browser (uses <a> tag). Not compatible with Node.js.
fix Use Node's built-in url module or whatwg-url for server-side.
gotcha Relative URLs resolve against current page's base URL, leading to unexpected results.
fix Always pass absolute URLs if you need consistent parsing.
deprecated Package is abandoned (last release 2013). No bug fixes or security updates.
fix Use built-in URL constructor (https://developer.mozilla.org/en-US/docs/Web/API/URL) or whatwg-url.
gotcha Returns query as string, not parsed object. No automatic query string parsing.
fix Use URLSearchParams or a query parser manually.
npm install component-url
yarn add component-url
pnpm add component-url

Parses a URL and demonstrates all properties plus utility checks.

var url = require('url');
var parsed = url.parse('http://example.com:3000/store/shoes?sort=desc');
console.log(parsed.protocol); // 'http:'
console.log(parsed.host); // 'example.com:3000'
console.log(parsed.port); // 3000
console.log(parsed.hostname); // 'example.com'
console.log(parsed.pathname); // '/store/shoes'
console.log(parsed.search); // '?sort=desc'
console.log(parsed.query); // 'sort=desc'
console.log(parsed.hash); // ''
console.log(parsed.href); // 'http://example.com:3000/store/shoes?sort=desc'
console.log(url.isAbsolute('http://a.com')); // true
console.log(url.isRelative('/foo')); // true
console.log(url.isCrossDomain('http://other.com')); // true (if page is on different domain)