The `universal-url` package provides a robust and consistent implementation of the WHATWG `URL` and `URLSearchParams` APIs, ensuring their availability across diverse JavaScript environments, including Node.js (versions 6 and above) and web browsers. This library intelligently determines whether to leverage the native `URL` implementation (available in Node.js versions `>= 8`) or to transparently apply a polyfill using the `whatwg-url` package for environments lacking native support, such as older Node.js versions (`< 8`) and some web browsers. The current stable version is 2.0.0. While a specific release cadence isn't detailed, updates typically align with bug fixes or evolutions in the underlying WHATWG URL specification. Its key differentiator is providing a singular, abstracted API layer that allows developers to write URL parsing and manipulation code once, confident that it will behave identically regardless of the execution environment, eliminating the need for conditional imports or polyfill management.
npm install universal-urlVerified import paths — ran on the pinned version, not inferred.
Demonstrates importing `URL` and `URLSearchParams` using CommonJS `require` and basic URL parsing, manipulation, and search parameter handling.
No direct fix needed as this is handled automatically. Be aware of potential subtle differences if profiling performance across environments.
Consider using `universal-url-lite` as an alias in your build configuration (e.g., Webpack, Rollup) for browser targets to reduce bundle size if native URL support is expected.
Ensure you either import `const {URL} = require('universal-url');` or apply the global shim via `require('universal-url').shim();` at the start of your application.Verify that the input string is a well-formed absolute or relative URL. For relative URLs, ensure a valid `base` URL is also provided (e.g., `new URL('/path', 'http://example.com/')`).If targeting modern browsers, alias `universal-url` to `universal-url-lite` in your build configuration to avoid including the unnecessary polyfill. For example, in Webpack: `resolve: { alias: { 'universal-url': 'universal-url-lite' } }`.