fjordtools / Errors / TypeError: Invalid URL

TypeError: Failed to construct 'URL': Invalid URL

The URL constructor only accepts a complete, absolute URL: a scheme such as https, then a host. A bare domain like example.com, a path such as /api, a missing colon, or a space in the host all fail. Add the scheme, or pass a base URL as the second argument for a relative path.

Fjordtools team · Seen in Chrome and Node (V8) · Published · 1 min read

How browsers word it

ChromeTypeError: Failed to construct 'URL': Invalid URL
NodeTypeError: Invalid URL
FirefoxTypeError: URL constructor: example.com is not a valid URL.

Common causes

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. Add the scheme: https://example.com.
  2. For a relative path, give a base: new URL("/api", "https://example.com").
  3. Check the value for undefined, an empty string and stray spaces before you build the URL.
  4. Paste the text into the URL encoder: it lists the parts when the URL is complete.

Related errors

More error explainers are coming

See all errors.