This recipe converts legacy Node.js url module usage to the modern WHATWG URL API, replacing methods like url.parse() and url.format() with the standard URL class.
Deprecation
Node.js deprecated the legacy url module methods in favor of the WHATWG URL Standard.
See DEP0116 for more details.
Usage
Run this codemod with:
Before/After
url.parse() to new URL()
What It Does
- Replaces
url.parse() with new URL()
- Replaces
url.format() with new URL().toString()
- Transforms legacy URL properties to WHATWG equivalents
- Updates imports to remove the
url module when no longer needed
Property Mapping
Caveats
url.resolve() Migration
The url.resolve() method is not directly translatable to the WHATWG URL API. You may need to implement custom logic:
Non-Standard Properties
If you’re using url.parse().auth, url.parse().path, or bracket-wrapped IPv6 hostnames, you’ll need to manually construct these values from WHATWG URL properties as shown in the examples above.
The WHATWG URL API doesn’t support all legacy url module features. Review the caveats section and test thoroughly after migration.
The WHATWG URL API is standardized across browsers and Node.js, making your code more portable and compatible with modern web standards.