denoland/deno · error · TypeError

Invalid URL: ${raw}

Error message

Invalid URL: ${raw}

What it means

In env mode (proxy config derived from the process environment via NODE_USE_ENV_PROXY=1 or --use-env-proxy), parseProxyUrl throws a plain TypeError 'Invalid URL: <raw>' when the value found for http_proxy/https_proxy is not a string. This mirrors Node v24, which reports bad env-derived proxy values as a TypeError rather than a coded Node error.

Source

Thrown at ext/node/polyfills/_http_proxy.js:108

}

function readEnvKey(env, keys) {
  for (let i = 0; i < keys.length; i++) {
    const k = keys[i];
    if (env[k] !== undefined && env[k] !== "") {
      return env[k];
    }
  }
  return undefined;
}

const CRLF_RE = new SafeRegExp(/[\r\n]/);

function parseProxyUrl(raw, kind, mode) {
  if (raw === undefined || raw === null || raw === "") return null;
  if (typeof raw !== "string") {
    if (mode === "env") {
      throw new TypeError(`Invalid URL: ${raw}`);
    }
    throw new ERR_PROXY_INVALID_CONFIG(
      `Invalid proxy URL for ${kind}: must be a string`,
    );
  }
  // CRLF injection guard - check raw string before URL parsing strips them.
  // Matches Node's CRLF rejection in the proxy URL validator. We surface this
  // even for env-derived URLs so the auth tests get the expected error class.
  if (RegExpPrototypeExec(CRLF_RE, raw) !== null) {
    throw new ERR_PROXY_INVALID_CONFIG(`Invalid proxy URL: ${raw}`);
  }
  let url;
  try {
    url = new URL(raw);
  } catch {
    if (mode === "env") {
      throw new TypeError(`Invalid URL: ${raw}`);
    }

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Ensure HTTP_PROXY/HTTPS_PROXY values are URL strings before enabling NODE_USE_ENV_PROXY
  2. Cast/coerce proxy env values to strings when composing env objects programmatically
  3. Wrap the first request in try/catch for TypeError and fall back to a direct connection if the proxy env is broken

Example fix

// before
const fakeEnv = { http_proxy: 8080 }; // number, not string
// with NODE_USE_ENV_PROXY=1 -> TypeError: Invalid URL: 8080 on first request

// after
const fakeEnv = { http_proxy: 'http://proxy.local:8080' };
Defensive patterns

Strategy: try-catch

Validate before calling

// before enabling env-proxy or issuing the first request
function proxyEnvTypesOk(env: Record<string, unknown>): boolean {
  return ['http_proxy','HTTP_PROXY','https_proxy','HTTPS_PROXY']
    .every((k) => env[k] === undefined || typeof env[k] === 'string');
}

Try / catch

try {
  await doRequest();
} catch (e) {
  if (e instanceof TypeError && /^Invalid URL:/.test(e.message)) {
    // broken proxy env: report config error clearly
    console.error('proxy env var is not a string:', e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: NODE_USE_ENV_PROXY active and a proxy variable resolves to a non-string - e.g. a programmatically composed env object (tests, custom harnesses calling buildProxyConfig in env mode) with http_proxy: 123. With the real process environment values are always strings, so this branch mostly fires through programmatic env injection. The error surfaces lazily on the first http/https request that initializes proxy state.

Common situations: Tests injecting fake env dictionaries with untyped values; wrappers that build env-like objects from JSON config where a number slips in.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/4ef9362f5abc5d91. Report an issue: GitHub.