denoland/deno · error · TypeError

Invalid port

Error message

Invalid port: ${maybePort}

What it means

The sibling branch of the same validator: the port converts to a non-integer through a path other than a NaN-producing string. This means the value itself is NaN, or it is fractional (8080.5), Infinity, or a string like '12.5'. The message interpolates the value without quotes, distinguishing it from the non-numeric-string case; it is a TypeError thrown before any network operation.

Solutions

  1. Initialize port variables to a concrete integer, never NaN
  2. Reject fractional computed values: if (Math.trunc(x) !== x) the input is wrong
  3. Guard with Number.isFinite(port) && Number.isInteger(port) before calling the API

Example fix

// before
const port = start + range / 2; // e.g. 8080.5 -> Invalid port: 8080.5
Deno.connect({ port });

// after
const port = start + Math.floor(range / 2);
if (!Number.isInteger(port)) throw new Error(`computed port is not an integer: ${port}`);
Deno.connect({ port });
Defensive patterns

Strategy: validation

Validate before calling

function toPort(value: number | string | undefined, fallback: number): number {
  const n = value === undefined ? fallback : Number(value);
  if (!Number.isInteger(n)) {
    throw new Error(`port must be an integer, received: ${String(value)}`);
  }
  return n;
}

Type guard

function isIntegerPort(p: unknown): p is number {
  return typeof p === 'number' && Number.isInteger(p) && Number.isFinite(p);
}

Try / catch

try {
  Deno.listen({ port });
} catch (e) {
  if (e instanceof TypeError && e.message.includes('Invalid port')) {
    // fractional/NaN input - fix the computation, do not retry
  }
  throw e;
}

Prevention

When it happens

Trigger: Deno.connect({ port: NaN }) from an uninitialized variable; Deno.listen({ port: 80.5 }); port computed as a / b with b = 0 yielding Infinity; Deno.connectTls({ port: '12.5' }).

Common situations: Optional config fields defaulted to NaN; division or averaging used to compute a port; float arithmetic or unit-conversion bugs producing fractional values; JSON config typos like 8080.5.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/4dc18f9b914b4f08. Report an issue: GitHub.

Appendix: source

Thrown at ext/net/01_net.js:653

}

function validatePort(maybePort, isServer = false) {
  // A missing port means "any available port" (port 0) for servers. Clients
  // must always specify a port to connect to, so a missing port is left as-is
  // and rejected below.
  if (isServer && (maybePort === null || maybePort === undefined)) {
    maybePort = 0;
  }
  if (typeof maybePort !== "number" && typeof maybePort !== "string") {
    throw new TypeError(`Invalid port (expected number): ${maybePort}`);
  }
  if (maybePort === "") throw new TypeError("Invalid port: ''");
  const port = Number(maybePort);
  if (!NumberIsInteger(port)) {
    if (NumberIsNaN(port) && !NumberIsNaN(maybePort)) {
      throw new TypeError(`Invalid port: '${maybePort}'`);
    } else {
      throw new TypeError(`Invalid port: ${maybePort}`);
    }
  } else if (port < (isServer ? 0 : 1) || port > 65535) {
    // Servers may bind to port 0 (OS-assigned), clients may not connect to it.
    throw new RangeError(`Invalid port (out of range): ${maybePort}`);
  }
  return port;
}

function createListenDatagram(udpOpFn, unixOpFn) {
  return function listenDatagram(args) {
    switch (args.transport) {
      case "udp": {
        const port = validatePort(args.port, true);
        const { 0: rid, 1: addr } = udpOpFn(
          {
            hostname: args.hostname ?? "0.0.0.0",
            port,
          },

View on GitHub (pinned to 9ad36f7a2c)