How browsers word it
| Chrome | TypeError: Failed to construct 'URL': Invalid URL |
|---|---|
| Node | TypeError: Invalid URL |
| Firefox | TypeError: URL constructor: example.com is not a valid URL. |
Common causes
The scheme is missing
A domain on its own is not a URL. Without https:// the parser has no scheme to work with.
Input
example.com
URL Encoder says
No parts are listed: this is not a complete URL
A relative path
A text that starts with a slash is relative. It needs a base: new URL(path, base).
Input
/path?x=1
URL Encoder says
No parts are listed: this is not a complete URL
A colon is missing
A typing slip in the scheme gives http// instead of http://.
Input
http//example.com
URL Encoder says
No parts are listed: this is not a complete URL
A space in the host
Spaces are allowed in a path, where they become %20, but not in a host name.
Input
https://exa mple.com
URL Encoder says
No parts are listed: this is not a complete URL
A complete URL works
With a scheme and a host the parts are read, and a space in the path is accepted.
Input
https://example.com/a b?x=1&y=2
URL Encoder says
Protocol https:, host example.com, path /a b, two parameters
How to fix it
- Add the scheme: https://example.com.
- For a relative path, give a base: new URL("/api", "https://example.com").
- Check the value for undefined, an empty string and stray spaces before you build the URL.
- Paste the text into the URL encoder: it lists the parts when the URL is complete.