{"record":{"id":"4f473d66e734a8e6","repo":"denoland/deno","slug":"invalid-port-expected-number-maybeport","errorCode":null,"errorMessage":"Invalid port (expected number): ${maybePort}","messagePattern":"Invalid port \\(expected number\\): (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":645,"sourceCode":"    }\n    case \"tunnel\": {\n      const { 0: rid, 1: addr } = op_net_listen_tunnel();\n      return new Listener(rid, addr, \"tunnel\");\n    }\n    default:\n      throw new TypeError(`Unsupported transport: '${transport}'`);\n  }\n}\n\nfunction validatePort(maybePort, isServer = false) {\n  // A missing port means \"any available port\" (port 0) for servers. Clients\n  // must always specify a port to connect to, so a missing port is left as-is\n  // and rejected below.\n  if (isServer && (maybePort === null || maybePort === undefined)) {\n    maybePort = 0;\n  }\n  if (typeof maybePort !== \"number\" && typeof maybePort !== \"string\") {\n    throw new TypeError(`Invalid port (expected number): ${maybePort}`);\n  }\n  if (maybePort === \"\") throw new TypeError(\"Invalid port: ''\");\n  const port = Number(maybePort);\n  if (!NumberIsInteger(port)) {\n    if (NumberIsNaN(port) && !NumberIsNaN(maybePort)) {\n      throw new TypeError(`Invalid port: '${maybePort}'`);\n    } else {\n      throw new TypeError(`Invalid port: ${maybePort}`);\n    }\n  } else if (port < (isServer ? 0 : 1) || port > 65535) {\n    // Servers may bind to port 0 (OS-assigned), clients may not connect to it.\n    throw new RangeError(`Invalid port (out of range): ${maybePort}`);\n  }\n  return port;\n}\n\nfunction createListenDatagram(udpOpFn, unixOpFn) {\n  return function listenDatagram(args) {","sourceCodeStart":627,"sourceCodeEnd":663,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/net/01_net.js#L627-L663","documentation":"validatePort() accepts only number or numeric-string ports; any other type — undefined/null from clients, booleans, objects, arrays — throws TypeError(`Invalid port (expected number): <value>`). Server-side listen() maps missing null/undefined to port 0 (OS-assigned), so this fires chiefly on connect paths and on structurally wrong values; the printed value usually pinpoints the bad field.","triggerScenarios":"`Deno.connect({ port: undefined })`; port: true; port: { port: 8080 } (double-nested config); passing the whole options object where only the port number was expected; an array from splitting a host string.","commonSituations":"Config typos like options.port.port; env parsing that yields undefined; destructuring the wrong property; defaults applied at the wrong layer so the port slot receives an object or boolean.","solutions":["Coerce once at the edge: const port = Number(cfg.port ?? 8080).","Inspect the value in the error message — '[object Object]' or 'undefined' identifies the mis-wired property; fix the property path.","Validate with a shared isPort helper before any connect/listen call."],"exampleFix":"// before\nDeno.connect({ hostname: \"db\", port: config.db }); // config.db = { host, port: 5432 }\n\n// after\nDeno.connect({ hostname: config.db.host, port: Number(config.db.port) });","handlingStrategy":"validation","validationCode":"function toPort(v: unknown, fallback?: number): number {\n  if (v === null || v === undefined || v === \"\") {\n    if (fallback === undefined) throw new TypeError(\"port missing\");\n    return fallback;\n  }\n  const n = Number(v);\n  if (!Number.isInteger(n) || n < 1 || n > 65535) {\n    throw new TypeError(`invalid port: ${String(v)}`);\n  }\n  return n;\n}\nDeno.connect({ hostname, port: toPort(cfg.port, 5432) });","typeGuard":"const isPortLike = (v: unknown): v is number | string =>\n  typeof v === \"number\" || typeof v === \"string\";","tryCatchPattern":null,"preventionTips":["Coerce ports once at the config boundary with Number() and a default","The error text shows the offending value — '[object Object]' means a wrong property path","Destructure exactly the numeric field you need instead of passing whole config objects"],"tags":["deno","net","port","connect","argument-validation"],"backgroundTag":"invalid-port","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}