DOM URLs for Node
dom-urls is a legacy Node.js package that provides a partial implementation of an older W3C URL Spec Draft. It was built on top of URIjs to address limitations in Node.js's native `url` module at the time (specifically, its inability to propagate changes between URL components like host and port). The package is designed for Node.js versions 0.8.0 and above. The current stable version is 1.1.0, released long ago. This package is effectively abandoned, as modern Node.js and browsers now provide a native, robust, and spec-compliant `URL` global object, rendering `dom-urls` obsolete. It lacks active development, modern features, and ESM support.
Common errors
-
TypeError: URL is not a constructor
cause Attempting to use `dom-urls` in an environment where `URL` is already defined globally (e.g., modern Node.js or browser) and you are trying to use the native `URL` without importing `dom-urls` or incorrectly overriding it.fixEnsure you are using `const URL = require('dom-urls');` correctly, and consider renaming the imported variable (e.g., `const DomURL = require('dom-urls');`) to avoid conflicts with the native `URL` global. Ideally, migrate to the native `URL` global. -
SyntaxError: Unexpected token 'export' (or 'import')
cause Attempting to use ESM `import` syntax for `dom-urls`, which is a CommonJS module.fixChange your import statement to CommonJS: `const URL = require('dom-urls');`
Warnings
- breaking This package is largely obsolete. Modern Node.js environments (v10.0.0+ as of 2018) include a global, spec-compliant `URL` class that fulfills the same purpose, but follows the WHATWG URL standard more rigorously.
- gotcha Error handling in `dom-urls` is explicitly stated in its README to be 'very different from the spec'. This can lead to unexpected behavior compared to the official WHATWG URL standard.
- deprecated This package is CommonJS-only and has not been updated in many years, indicating it is no longer maintained. It may have unpatched vulnerabilities or incompatibilities with modern Node.js versions.
Install
-
npm install dom-urls -
yarn add dom-urls -
pnpm add dom-urls
Imports
- URL
import { URL } from 'dom-urls'; import URL from 'dom-urls';const URL = require('dom-urls');
Quickstart
const URL = require('dom-urls');
// Create a new URL object with a relative path and a base URL
const url = new URL('relative', 'http://example.com/sub/');
console.log('Original URL:', url.href); // Should be 'http://example.com/sub/relative/'
console.log('Protocol:', url.protocol); // 'http:'
console.log('Hostname:', url.hostname); // 'example.com'
console.log('Pathname:', url.pathname); // '/sub/relative/'
// Modify the host and observe changes
url.host = 'example.net:8080';
console.log('Modified URL:', url.href); // Should be 'http://example.net:8080/sub/relative/'
console.log('New Hostname:', url.hostname); // 'example.net'
console.log('New Port:', url.port); // '8080'