{"record":{"id":"0928a6e5d265919f","repo":"denoland/deno","slug":"invalid-port","errorCode":null,"errorMessage":"Invalid port: ''","messagePattern":"Invalid port: ''","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":647,"sourceCode":"      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) {\n    switch (args.transport) {\n      case \"udp\": {","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/net/01_net.js#L629-L665","documentation":"An empty-string port is rejected outright with TypeError(\"Invalid port: ''\"). The dedicated check exists because Number(\"\") === 0, which would otherwise silently bind or connect to port 0 — an OS-assigned or invalid target — instead of surfacing the missing value as an error.","triggerScenarios":"`port: Deno.env.get(\"PORT\")` when PORT is set but empty; `new URL(\"https://example.com/\").port` which is \"\" when the URL carries no explicit port; splitting \"host:\" on \":\"; template `${cfg.port}` when cfg.port is an empty string.","commonSituations":"PORT env var defined-but-empty in .env files or CI variable passthrough; URLs without ports where the scheme default was expected; config merges that leave \"\" instead of undefined.","solutions":["Default falsy to a concrete port: const port = Number(Deno.env.get(\"PORT\") || 3000).","For URLs, fall back to the scheme default: url.port || (url.protocol === \"https:\" ? \"443\" : \"80\").","Validate config at startup and fail fast with a clear message before any socket call."],"exampleFix":"// before\nconst port = Deno.env.get(\"PORT\") ?? \"\"; // PORT defined but empty\nDeno.listen({ port }); // Invalid port: ''\n\n// after\nconst port = Number(Deno.env.get(\"PORT\") || 3000);\nDeno.listen({ port });","handlingStrategy":"validation","validationCode":"const envPort = Deno.env.get(\"PORT\");\nconst port = Number(envPort && envPort.trim() !== \"\" ? envPort : 3000);\nif (!Number.isInteger(port) || port < 1 || port > 65535) {\n  throw new Error(`invalid PORT configuration: ${JSON.stringify(envPort)}`);\n}","typeGuard":"const isNonEmptyPortString = (v: unknown): v is string =>\n  typeof v === \"string\" && v.trim() !== \"\" && Number.isInteger(Number(v));","tryCatchPattern":null,"preventionTips":["Treat empty PORT as unset: `Deno.env.get(\"PORT\") || 3000`","URL ports are \"\" when absent — map to the scheme default (443/80) explicitly","Fail fast on bad port config at startup instead of at first listen/connect"],"tags":["deno","net","port","env-var","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"}