sindresorhus / normalize-url

Normalize a URL
MIT License
840 stars 122 forks source link

Fix "URL is not a constructor" error in the browser #61

Closed mariosant closed 6 years ago

mariosant commented 6 years ago

This should fix #60.

Unfortunately, I haven't found a way to test it under browser environment. Any help would be appreciated.

sindresorhus commented 6 years ago

I think a conditional would be better. That way we won't bundle the whole url lib in the browser, and when we target Node.js 10, we can remove the require.

mariosant commented 6 years ago

More like that, @sindresorhus ?

sindresorhus commented 6 years ago

No, Node.js 10 also has the URL global, so the conditional should be whether the URL global already exists.

const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
mariosant commented 6 years ago

Gotcha! I believe this does the trick @sindresorhus.

sindresorhus commented 6 years ago

Looks good :)