zloirock/core-js · error · TypeError

<dynamic URL parse failure message> (dynamic: throw new Type

Error message

<dynamic URL parse failure message> (dynamic: throw new TypeError(failure))

What it means

TypeError raised in the URLState constructor (the internal state behind the URL polyfill) when parsing a URL as a base: this.parse(urlString) returns a failure string produced by the internal URL parser, and any non-null failure is thrown as a TypeError. It means the first argument is not a valid absolute URL.

Source

Thrown at packages/core-js/modules/web.url.constructor.js:431

var AUTHORITY = {};
var HOST = {};
var HOSTNAME = {};
var PORT = {};
var FILE = {};
var FILE_SLASH = {};
var FILE_HOST = {};
var PATH_START = {};
var PATH = {};
var CANNOT_BE_A_BASE_URL_PATH = {};
var QUERY = {};
var FRAGMENT = {};

var URLState = function (url, isBase, base) {
  var urlString = $toString(url);
  var baseState, failure, searchParams;
  if (isBase) {
    failure = this.parse(urlString);
    if (failure) throw new TypeError(failure);
    this.searchParams = null;
  } else {
    if (base !== undefined) baseState = new URLState(base, true);
    failure = this.parse(urlString, null, baseState);
    if (failure) throw new TypeError(failure);
    searchParams = getInternalSearchParamsState(new URLSearchParams());
    searchParams.bindURL(this);
    this.searchParams = searchParams;
  }
};

URLState.prototype = {
  type: 'URL',
  // https://url.spec.whatwg.org/#url-parsing
  // eslint-disable-next-line max-statements -- TODO
  parse: function (input, stateOverride, base) {
    var url = this;
    var state = stateOverride || SCHEME_START;

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Validate/normalize the URL string before constructing: check it has a scheme or use URL.canParse() where available
  2. Prepend a default scheme when the input lacks one (e.g. 'https://' + host)
  3. Wrap construction in try/catch and surface a friendlier error
  4. Trim whitespace and encode illegal characters first

Example fix

// before
const url = new URL(userInput); // TypeError on 'example.com/page'
// after
const raw = /^\w+:\/\//.test(userInput) ? userInput : 'https://' + userInput;
const url = new URL(raw);
Defensive patterns

Strategy: try-catch

Validate before calling

function safeURL(input, base) {
  if (typeof input !== 'string' || input.trim() === '') throw new Error('URL required');
  if (typeof URL.canParse === 'function' && !URL.canParse(input)) throw new Error('Not an absolute URL: ' + input);
  return new URL(input, base);
}

Type guard

const looksAbsolute = (s) =>
  typeof s === 'string' && /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(s.trim());

Try / catch

let url;
try {
  url = new URL(input);
} catch (e) {
  if (e instanceof TypeError) {
    url = new URL('https://' + input.replace(/^\/+/, ''));
  } else throw e;
}

Prevention

When it happens

Trigger: new URL('not a url') or new URL('') — the string fails the WHATWG URL parser (no scheme, invalid characters, bad IPv4/IPv6 host, etc.); also new URL(url, base) where url is relative but base itself is valid — that path throws at line 436, not here.

Common situations: User-supplied or API-returned strings that are not absolute URLs; empty strings from config/env; URLs with spaces or unencoded characters; concatenating strings and forgetting the scheme ('example.com/x' without 'https://').

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/8c45a520a50a87c4. Report an issue: GitHub.