denoland/deno · error · TypeError

Invalid port: ${maybePort}

Error message

Invalid port: ${maybePort}

What it means

The sibling branch of validatePort(): thrown when the numeric conversion of the port is not an integer but the input is not an unparseable string - i.e. the literal NaN value or a fractional number/string such as 8080.5. The unquoted message form distinguishes these from garbage strings.

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 89f33cbef2)

Solutions

  1. Trace the NaN or fraction to its source (failed parse, arithmetic on undefined) and fix it there
  2. Round computed ports with Math.floor() or Math.round() before passing them
  3. For random ports use 1 + Math.floor(Math.random() * 65535)

Example fix

// before
const port = basePort + Math.random() * 100;
await Deno.connect({ hostname, port }); // TypeError: Invalid port: 8080.47...

// after
const port = Math.floor(basePort + Math.random() * 100);
await Deno.connect({ hostname, port });
Defensive patterns

Strategy: validation

Validate before calling

function assertIntegerPort(port: unknown): asserts port is number {
  const n = Number(port);
  if (!Number.isInteger(n)) {
    throw new Error(
      `Port must be an integer, got: ${typeof port === "string" ? JSON.stringify(port) : String(port)}`,
    );
  }
}

Type guard

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

Prevention

When it happens

Trigger: Deno.connect({ port: NaN }) after a calculation on undefined; port: 8080.5 from fractional arithmetic; port: '63.5'; expressions like Math.random() * 65535 passed without Math.floor.

Common situations: NaN propagation from failed parseInt or optional-chain arithmetic on undefined; computing ports with offsets/halving; random port selection missing a floor/round step.

Related errors


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