Node.js Alternative after url.parse and querystring Deprecated

tanut aran
CODEMONDAY
Published in
1 min readJul 28, 2021

--

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('https://developer.mozilla.org/en-US/docs/Web/API/URL')Url {                                                                                                                     protocol: 'https:',                                                                                                     slashes: true,                                                                                                          auth: null,                                                                                                             host: 'developer.mozilla.org',                                                                                          port: null,                                                                                                             hostname: 'developer.mozilla.org',                                                                                      hash: null,                                                                                                             search: null,                                                                                                           query: null,                                                                                                            pathname: '/en-US/docs/Web/API/URL',                                                                                    path: '/en-US/docs/Web/API/URL',                                                                                        href: 'https://developer.mozilla.org/en-US/docs/Web/API/URL'                                                          }

Now use URL

> new URL('https://developer.mozilla.org/en-US/docs/Web/API/URL')URL {                                                                                                                     href: 'https://developer.mozilla.org/en-US/docs/Web/API/URL',                                                           origin: 'https://developer.mozilla.org',
protocol: 'https:',
username: '',
password: '',
host: 'developer.mozilla.org',
hostname: 'developer.mozilla.org',
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' }                                                                                                                                                   > query.foo                                                                                                                                                                                           '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 !

--

--