{"record":{"id":"90fa34e43c76377a","repo":"denoland/deno","slug":"err-invalid-arg-type-90fa34","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"options.port\" property must be one of type number or string","messagePattern":"The \"options\\.port\" property must be one of type number or string","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/net.ts","lineNumber":1069,"sourceCode":"\nfunction _lookupAndConnect(self: Socket, options: TcpSocketConnectOptions) {\n  const { localAddress, localPort } = options;\n  const host = options.host || \"localhost\";\n  let { port, autoSelectFamilyAttemptTimeout, autoSelectFamily } = options;\n\n  validateStringWithoutNullBytes(host, \"options.host\");\n\n  if (localAddress && !isIP(localAddress)) {\n    throw new ERR_INVALID_IP_ADDRESS(localAddress);\n  }\n\n  if (localPort) {\n    validateNumber(localPort, \"options.localPort\");\n  }\n\n  if (typeof port !== \"undefined\") {\n    if (typeof port !== \"number\" && typeof port !== \"string\") {\n      throw new ERR_INVALID_ARG_TYPE(\n        \"options.port\",\n        [\"number\", \"string\"],\n        port,\n      );\n    }\n\n    validatePort(port);\n  }\n\n  port |= 0;\n\n  if (autoSelectFamily != null) {\n    validateBoolean(autoSelectFamily, \"options.autoSelectFamily\");\n  } else {\n    autoSelectFamily = autoSelectFamilyDefault;\n  }\n\n  if (autoSelectFamilyAttemptTimeout !== undefined) {","sourceCodeStart":1051,"sourceCodeEnd":1087,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/net.ts#L1051-L1087","documentation":"In net.connect()'s connect path, once options.port is defined it must be a number or a string (numeric string); validatePort and the port |= 0 truncation follow. Any other type throws ERR_INVALID_ARG_TYPE — notably null (typeof null === 'object'), plain objects, booleans, and BigInts.","triggerScenarios":"net.connect({ port: null }) from an optional config field defaulting to null; port: {} after wrong destructuring; port: true from a flag; port: 8080n (BigInt) — strings like '3000' are fine, undefined skips the check.","commonSituations":"JSON/YAML configs where an absent port serializes as null instead of undefined; TypeScript code assuming optional number but receiving null at runtime from an API response; env parsing that yields objects on failure.","solutions":["Normalize before connecting: const port = rawPort ?? DEFAULT_PORT; net.connect({ port: Number(port) })","Validate the type at the config boundary: reject port values that are neither number nor numeric string","Use undefined (omit the field) when there is no port, never null"],"exampleFix":"// before\nnet.connect({ port: config.port, host }); // config.port === null -> throws\n\n// after\nnet.connect({\n  port: config.port == null ? DEFAULT_PORT : Number(config.port),\n  host,\n});","handlingStrategy":"validation","validationCode":"if (port !== undefined && typeof port !== 'number' && typeof port !== 'string') {\n  throw new TypeError(`options.port must be number|string, got ${typeof port}`);\n}\nnet.connect({ port: port == null ? DEFAULT_PORT : Number(port), host });","typeGuard":"function isValidPortValue(v: unknown): v is number | string | undefined {\n  return v === undefined || typeof v === 'number' || typeof v === 'string';\n}","tryCatchPattern":"try {\n  socket = net.connect({ port, host });\n} catch (e: any) {\n  if (e?.code === 'ERR_INVALID_ARG_TYPE' && /options\\.port/.test(e.message)) {\n    socket = net.connect({ port: DEFAULT_PORT, host });\n  } else throw e;\n}","preventionTips":["Normalize optional config ports with ?? DEFAULT_PORT before passing them on","Parse env/config ports once (Number(v)) at the boundary, not at each call site","Use undefined for absent values — null is a type error for options.port"],"tags":["network","sockets","argument-validation","node-compat"],"backgroundTag":"invalid-argument-value","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}