Published in


Node.js Alternative after url.parse and querystring Deprecated

Since url.parse and querystring.parse is deprecated here and here at least since last LTS version 14.

The doc doesn’t suggest alternatives, so I study and found the standard alternative here below:

First on URL

Legacy is url.parse

> url.parse('')Url {                                                                                                                     protocol: 'https:',                                                                                                     slashes: true,                                                                                                          auth: null,                                                                                                             host: '',                                                                                          port: null,                                                                                                             hostname: '',                                                                                      hash: null,                                                                                                             search: null,                                                                                                           query: null,                                                                                                            pathname: '/en-US/docs/Web/API/URL',                                                                                    path: '/en-US/docs/Web/API/URL',                                                                                        href: ''                                                          }

Now use URL

> new URL('')URL {                                                                                                                     href: '',                                                           origin: '',
protocol: 'https:',
username: '',
password: '',
host: '',
hostname: '',
port: '',
pathname: '/en-US/docs/Web/API/URL',
search: '',
searchParams: URLSearchParams {},
hash: ''

This one is the Web API standard. You can find the same in the browser as well. So, it’s better to comply with standard over Node own library.

Second on querystring

Legacy is querystring.parse

> query = querystring.parse('foo=bar&bar=baz')                                                                                                                                                        [Object: null prototype] { foo: 'bar', bar: 'baz' }                                                                                                                                                   >                                                                                                                                                                                           'bar'

Now use URLSearchParams

> new URLSearchParams('&foo=bar&bar=baz')                                                                                                                                                              URLSearchParams { 'foo' => 'bar', 'bar' => 'baz' }                                                                                                                                                    > foo = new URLSearchParams('&foo=bar&bar=baz')                                                                                                                                                        URLSearchParams { 'foo' => 'bar', 'bar' => 'baz' }                                                                                                                                                    > foo.get('bar')                                                                                                                                                                                      'baz'

Same reason here, you can also find URLSearchParams in the browser.

Hope this help. Cheers !



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store