Nnodejs

nodejs/node-url-to-whatwg-url

Handle DEP0116 via transforming `url.parse` to `new URL()`

transformationmigration
Public
103 executions
Run locally
npx codemod @nodejs/node-url-to-whatwg-url
Documentation

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()

diff

url.format to `myUrl.toString()

diff

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.

Ready to contribute?

Build your own codemod and share it with the community.