{"id":"a7e00594280b9423","repo":"mongodb/node-mongodb-native","slug":"must-specify-proxyhost-if-other-proxy-options-are","errorCode":null,"errorMessage":"Must specify proxyHost if other proxy options are passed","messagePattern":"Must specify proxyHost if other proxy options are passed","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":508,"sourceCode":"      urlOptions.has('srvServiceName') ||\n      objectOptions.has('srvServiceName');\n\n    if (userSpecifiedSrvOptions) {\n      throw new MongoParseError(\n        'Cannot use srvMaxHosts or srvServiceName with a non-srv connection string'\n      );\n    }\n  }\n\n  if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {\n    throw new MongoParseError('directConnection option requires exactly one host');\n  }\n\n  if (\n    !mongoOptions.proxyHost &&\n    (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)\n  ) {\n    throw new MongoParseError('Must specify proxyHost if other proxy options are passed');\n  }\n\n  if (\n    (mongoOptions.proxyUsername && !mongoOptions.proxyPassword) ||\n    (!mongoOptions.proxyUsername && mongoOptions.proxyPassword)\n  ) {\n    throw new MongoParseError('Can only specify both of proxy username/password or neither');\n  }\n\n  const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map(\n    key => urlOptions.get(key) ?? []\n  );\n\n  if (proxyOptions.some(options => options.length > 1)) {\n    throw new MongoParseError(\n      'Proxy options cannot be specified multiple times in the connection string'\n    );\n  }","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L490-L526","documentation":"Thrown when proxyPort, proxyUsername, or proxyPassword is set but proxyHost is missing (connection_string.ts:504-509). proxyHost is the anchor of the SOCKS/HTTP proxy configuration; without it the other fields are uninterpretable, so the driver refuses to construct an incomplete proxy spec.","triggerScenarios":"URI like 'mongodb://host/?proxyPort=1080&proxyUsername=foo' or options { proxyPort: 1080 } without proxyHost.","commonSituations":"Building proxy options conditionally (e.g. setting proxyPort only when behind a corporate proxy) and forgetting the host; typos like proxyHot vs proxyHost.","solutions":["Add proxyHost to the URI or options whenever any other proxy option is present.","Double-check spelling: proxyHost, proxyPort, proxyUsername, proxyPassword.","If you don't need a proxy, remove all proxy options."],"exampleFix":"// before\nconst c = new MongoClient(uri, { proxyPort: 1080, proxyUsername: 'u', proxyPassword: 'p' });\n// after\nconst c = new MongoClient(uri, { proxyHost: 'proxy.example', proxyPort: 1080, proxyUsername: 'u', proxyPassword: 'p' });","handlingStrategy":"validation","validationCode":"const proxyKeys = ['proxyPort', 'proxyUsername', 'proxyPassword'];\nconst anyProxySecondary = proxyKeys.some(k => k in opts || new URL(uri).searchParams.has(k));\nif (anyProxySecondary && !('proxyHost' in opts) && !new URL(uri).searchParams.has('proxyHost')) {\n  throw new Error('proxyHost is required when any other proxy option is set');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat proxy options as all-or-nothing bundles.","Validate proxy config in a dedicated helper used by all callers."],"tags":["connection-string","proxy","configuration"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}