{"record":{"id":"f13359b6f7046734","repo":"denoland/deno","slug":"err-invalid-ip-address-f13359","errorCode":"ERR_INVALID_IP_ADDRESS","errorMessage":"Invalid IP address: ${localAddress}","messagePattern":"Invalid IP address: (.+?)","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/net.ts","lineNumber":1060,"sourceCode":"        }\n\n        socket[kBuffer] = userBuf;\n      }\n\n      socket._handle.useUserBuffer(userBuf);\n    }\n  }\n}\n\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","sourceCodeStart":1042,"sourceCodeEnd":1078,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/net.ts#L1042-L1078","documentation":"During net.connect(), _lookupAndConnect validates options.localAddress with isIP(): it must be a local IP literal, because the value is passed straight to bind() — no DNS resolution happens for it. A hostname (even 'localhost') or malformed IP throws ERR_INVALID_IP_ADDRESS before the connection starts.","triggerScenarios":"net.connect({ port, host: 'example.com', localAddress: 'example.com' }); localAddress: 'localhost' (a hostname, not an IP); localAddress: '192.168.1.999'; a config/env var supplying a hostname into localAddress.","commonSituations":"Multi-homed servers binding egress traffic to a specific NIC by name instead of address; copying the host option's semantics onto localAddress; IPv6 addresses missing or misplaced (e.g. missing brackets does not matter, but a bare zone-id does).","solutions":["Pass an IP literal for localAddress: '127.0.0.1', '10.0.0.5', '::1'","If the source is a hostname, resolve it first with dns.lookup(host) and use the returned address","Pre-validate with net.isIP(localAddress) !== 0 and fall back to default (omit localAddress)"],"exampleFix":"// before\nnet.connect({ port, host, localAddress: cfg.localHost }); // hostname -> throws\n\n// after\nconst localAddress = net.isIP(cfg.localHost) ? cfg.localHost : undefined;\nnet.connect({ port, host, localAddress });","handlingStrategy":"validation","validationCode":"const localAddress = net.isIP(cfg.localHost ?? '') !== 0\n  ? cfg.localHost\n  : undefined; // omit rather than pass a hostname\nnet.connect({ port, host, localAddress });","typeGuard":"function isIpLiteral(v: unknown): v is string {\n  return typeof v === 'string' && net.isIP(v) !== 0;\n}","tryCatchPattern":"try {\n  socket = net.connect({ port, host, localAddress });\n} catch (e: any) {\n  if (e?.code === 'ERR_INVALID_IP_ADDRESS') {\n    socket = net.connect({ port, host }); // retry without localAddress binding\n  } else throw e;\n}","preventionTips":["localAddress takes IP literals only — resolve hostnames with dns.lookup first","Validate config-provided local addresses with net.isIP() at load time","Do not copy the host option's hostname semantics onto localAddress"],"tags":["network","sockets","ip-address","argument-validation"],"backgroundTag":"invalid-ip-address","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"}