Node.js URL to WHATWG URL
This recipe converts Node.js url module usage to the WHATWG URL API. It modifies code that uses the legacy url module to use the modern URL class instead.
See DEP0116.
Example
url.parse to new URL()
Before:
js
After:
js
url.format to `myUrl.toString()
Before:
js
After:
js
Note: The migration of url.format can also be done as `${new URL('https://example.com/some/path?page=1&format=json')}` which is little bit more efficient. But it may be less readable for some users.
Caveats
The url.resolve method is not directly translatable to the WHATWG URL API. You may need to implement custom logic to handle URL resolution in your application.
js
If you are using url.parse().auth, url.parse().username, or url.parse().password. Will transform to new URL().auth which is not valid WHATWG url property. So you have to manually construct the auth, path, and hostname properties as shown in the examples above.