{"record":{"id":"65b0339961b5abc2","repo":"denoland/deno","slug":"invalid-port-maybeport","errorCode":null,"errorMessage":"Invalid port: '${maybePort}'","messagePattern":"Invalid port: '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":651,"sourceCode":"      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) {\n    switch (args.transport) {\n      case \"udp\": {\n        const port = validatePort(args.port, true);\n        const { 0: rid, 1: addr } = udpOpFn(\n          {\n            hostname: args.hostname ?? \"0.0.0.0\",","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/net/01_net.js#L633-L669","documentation":"Thrown by Deno's port validator (validatePort in ext/net/01_net.js) when the port is a string that cannot be converted to a number: Number(maybePort) is NaN while the input itself is not NaN. It is a TypeError raised before any socket is created. Every port passed to Deno.connect, Deno.listen, Deno.connectTls, and Deno.listenDatagram goes through this check.","triggerScenarios":"Deno.connect({ hostname: 'db', port: 'localhost' }) (hostname value placed in the port field); Deno.listen({ port: '8080a' }); Deno.listenDatagram({ transport: 'udp', port: 'auto' }) where a non-numeric env/config string is forwarded.","commonSituations":"Swapping hostname and port arguments; reading the port from an environment variable or config file that contains text like 'auto', 'default', or 'PORT'; passing a full URL string ('localhost:5432') instead of just the port component.","solutions":["Pass the port as a number: Deno.connect({ hostname, port: 5432 })","Coerce values from env/config at the boundary: const port = Number(raw); if (!Number.isInteger(port)) fail fast with a clear config error","If the value comes from a URL, extract it with new URL(str).port and Number() it before calling"],"exampleFix":"// before\nDeno.connect({ port: Deno.env.get('PORT') }); // PORT='auto' -> Invalid port: 'auto'\n\n// after\nconst raw = Deno.env.get('PORT') ?? '8080';\nconst port = Number(raw);\nif (!Number.isInteger(port) || port < 1 || port > 65535) {\n  throw new Error(`Invalid PORT value: ${raw}`);\n}\nDeno.connect({ port });","handlingStrategy":"validation","validationCode":"function parsePort(raw: unknown): number {\n  if (typeof raw !== 'number' && typeof raw !== 'string') throw new TypeError('port must be number or string');\n  if (raw === '') throw new TypeError('port must not be empty');\n  const n = Number(raw);\n  if (!Number.isInteger(n)) throw new TypeError(`Invalid port: ${String(raw)}`);\n  return n;\n}\n// const port = parsePort(Deno.env.get('PORT')); Deno.connect({ port });","typeGuard":"function isValidPortSpec(p: unknown): p is number | string {\n  if (p === '' || p === null || p === undefined) return false;\n  return Number.isInteger(Number(p));\n}","tryCatchPattern":"try {\n  await Deno.connect({ port });\n} catch (e) {\n  if (e instanceof TypeError && e.message.startsWith('Invalid port')) {\n    throw new Error(`Bad port in configuration: ${JSON.stringify(portArg)}`);\n  }\n  throw e;\n}","preventionTips":["Coerce env/config port values with Number() once at the config boundary and store only numbers afterwards","Type config as { port: number } so TypeScript rejects strings at compile time","Log the raw port value whenever validation fails so misconfigured keys are obvious"],"tags":["deno","network","port","typeerror","validation"],"backgroundTag":"invalid-port-number","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"}