{"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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/net/01_net.js#L633-L669","documentation":"Thrown by Deno's internal validatePort() (ext/net/01_net.js) when the port argument is a string that Number() cannot parse (converts to NaN) while the original value itself is not NaN. The quoted form in the message distinguishes garbage strings like 'abc' from NaN or fractional inputs, which take the unquoted variant.","triggerScenarios":"Any port-validating Deno net API - Deno.connect, Deno.listen, Deno.listenTls, Deno.connectTls, Deno.listenDatagram - called with port set to a non-numeric, non-empty string: Deno.connect({ hostname: 'example.com', port: 'https' }), port: '8080 ' (trailing space), or port: 'eighty'.","commonSituations":"PORT read from Deno.env.get() or process.argv without Number() conversion; ports assembled from config strings with suffixes; accidentally swapping hostname and port fields; URL parsing that yields non-numeric fragments.","solutions":["Convert the value before the call: const port = Number(rawPort) (or parseInt(rawPort, 10)) and pass the number","Validate at the config boundary (env var, CLI flag, config file) and fail fast with your own clear error","When deriving a port from a URL, remember new URL(u).port is '' for scheme-default ports - fall back to 80/443 explicitly"],"exampleFix":"// before\nconst port = Deno.env.get(\"PORT\");\nawait Deno.connect({ hostname: \"db.local\", port }); // TypeError: Invalid port: '...'\n\n// after\nconst port = Number(Deno.env.get(\"PORT\"));\nif (!Number.isInteger(port) || port < 1 || port > 65535) {\n  throw new Error(`Invalid PORT value: ${Deno.env.get(\"PORT\")}`);\n}\nawait Deno.connect({ hostname: \"db.local\", port });","handlingStrategy":"validation","validationCode":"function parsePort(raw: string | number | undefined): number {\n  const port = Number(raw);\n  if (!Number.isInteger(port) || port < 1 || port > 65535) {\n    throw new Error(`Invalid port value: ${String(raw)}`);\n  }\n  return port;\n}\n\n// before any Deno.connect / Deno.listen call:\nawait Deno.connect({ hostname, port: parsePort(cfg.port) });","typeGuard":"function isValidPortValue(p: unknown): p is number {\n  return typeof p === \"number\" && Number.isInteger(p) && p >= 0 && p <= 65535;\n}","tryCatchPattern":null,"preventionTips":["Convert and validate ports once at startup (env vars, CLI flags, config) rather than at each call site","Type network options as { port: number } in TypeScript so string ports fail to compile","Never forward URL components blindly - url.port is '' for scheme-default ports"],"tags":["network","port","validation","environment-variables","deno"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}