{"record":{"id":"36e22c514325014a","repo":"mongodb/node-mongodb-native","slug":"option-srvhost-must-not-be-empty","errorCode":null,"errorMessage":"Option \"srvHost\" must not be empty","messagePattern":"Option \"srvHost\" must not be empty","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":79,"sourceCode":"        throw firstDNSError;\n      }\n    }\n  };\n}\n\nconst resolveSrv = retryDNSTimeoutFor('SRV');\nconst resolveTxt = retryDNSTimeoutFor('TXT');\n\n/**\n * Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal\n * connection string.\n *\n * @param uri - The connection string to parse\n * @param options - Optional user provided connection string options\n */\nexport async function resolveSRVRecord(options: MongoOptions): Promise<HostAddress[]> {\n  if (typeof options.srvHost !== 'string') {\n    throw new MongoAPIError('Option \"srvHost\" must not be empty');\n  }\n\n  // Asynchronously start TXT resolution so that we do not have to wait until\n  // the SRV record is resolved before starting a second DNS query.\n  const lookupAddress = options.srvHost;\n  const txtResolutionPromise = resolveTxt(lookupAddress);\n\n  txtResolutionPromise.then(undefined, squashError); // rejections will be handled later\n\n  const hostname = `_${options.srvServiceName}._tcp.${lookupAddress}`;\n  // Resolve the SRV record and use the result as the list of hosts to connect to.\n  const addresses = await resolveSrv(hostname);\n\n  if (addresses.length === 0) {\n    throw new MongoAPIError('No addresses found at host');\n  }\n\n  for (const { name } of addresses) {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L61-L97","documentation":"Thrown by resolveSRVRecord when options.srvHost is not a string. For mongodb+srv connection strings the driver must have a non-empty srvHost (the hostname portion) to perform DNS SRV/TXT lookups; a missing or non-string value is a configuration error and raises a MongoAPIError before any DNS query.","triggerScenarios":"Fires at src/connection_string.ts:79 when `typeof options.srvHost !== 'string'`. Reachable whenever a mongodb+srv URI is used but internal option resolution produced a non-string srvHost, or when resolveSRVRecord is invoked with malformed options.","commonSituations":"Using a mongodb+srv URI whose host is empty (e.g. 'mongodb+srv:///dbname'); programmatically building options where srvHost was deleted/undefined; a connection-string parsing edge case; a bug in custom MongoClientOptions construction. For end users the URI itself is the usual culprit.","solutions":["Ensure the mongodb+srv URI has a non-empty hostname: mongodb+srv://cluster0.example.mongodb.net/dbname.","If constructing options manually, set srvHost to a valid hostname string.","Switch to a standard mongodb:// URI with explicit hosts if SRV is not required.","Validate the URI string before passing it to MongoClient."],"exampleFix":"// before\nconst client = new MongoClient('mongodb+srv:///mydb');\n\n// after\nconst client = new MongoClient('mongodb+srv://cluster0.example.mongodb.net/mydb');","handlingStrategy":"validation","validationCode":"function validateSrvUri(uri: string) {\n  if (uri.startsWith('mongodb+srv://')) {\n    const host = uri.slice('mongodb+srv://'.length).split('/')[0].split('?')[0];\n    if (!host) throw new Error('mongodb+srv URI is missing a hostname');\n  }\n}\nvalidateSrvUri(uri);\nconst client = new MongoClient(uri);","typeGuard":"function hasSrvHost(uri: string): boolean {\n  if (!uri.startsWith('mongodb+srv://')) return true;\n  const rest = uri.slice('mongodb+srv://'.length);\n  const host = rest.split(/[/?]/)[0];\n  return host.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Ensure mongodb+srv URIs include a non-empty hostname.","Use mongodb:// with explicit hosts if SRV is unnecessary.","Validate connection strings before passing to MongoClient."],"tags":["connection-string","srv","dns","configuration"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}