mongodb/node-mongodb-native · error · MongoAPIError
Option "srvHost" must not be empty
Error message
Option "srvHost" must not be empty
What it means
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.
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.
Example fix
// before
const client = new MongoClient('mongodb+srv:///mydb');
// after
const client = new MongoClient('mongodb+srv://cluster0.example.mongodb.net/mydb'); Defensive patterns
Strategy: validation
Validate before calling
function validateSrvUri(uri: string) {
if (uri.startsWith('mongodb+srv://')) {
const host = uri.slice('mongodb+srv://'.length).split('/')[0].split('?')[0];
if (!host) throw new Error('mongodb+srv URI is missing a hostname');
}
}
validateSrvUri(uri);
const client = new MongoClient(uri); Type guard
function hasSrvHost(uri: string): boolean {
if (!uri.startsWith('mongodb+srv://')) return true;
const rest = uri.slice('mongodb+srv://'.length);
const host = rest.split(/[/?]/)[0];
return host.length > 0;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Options for SrvPoller must exist and include srvHost
- Cannot have empty URI params in DNS TXT Record
- Multiple text records not allowed
- No addresses found at host
- Server record does not have at least one more domain level…
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/36e22c514325014a.
Report an issue: GitHub.
Appendix: source
Thrown at src/connection_string.ts:79
throw firstDNSError;
}
}
};
}
const resolveSrv = retryDNSTimeoutFor('SRV');
const resolveTxt = retryDNSTimeoutFor('TXT');
/**
* Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
* connection string.
*
* @param uri - The connection string to parse
* @param options - Optional user provided connection string options
*/
export async function resolveSRVRecord(options: MongoOptions): Promise<HostAddress[]> {
if (typeof options.srvHost !== 'string') {
throw new MongoAPIError('Option "srvHost" must not be empty');
}
// Asynchronously start TXT resolution so that we do not have to wait until
// the SRV record is resolved before starting a second DNS query.
const lookupAddress = options.srvHost;
const txtResolutionPromise = resolveTxt(lookupAddress);
txtResolutionPromise.then(undefined, squashError); // rejections will be handled later
const hostname = `_${options.srvServiceName}._tcp.${lookupAddress}`;
// Resolve the SRV record and use the result as the list of hosts to connect to.
const addresses = await resolveSrv(hostname);
if (addresses.length === 0) {
throw new MongoAPIError('No addresses found at host');
}
for (const { name } of addresses) {View on GitHub (pinned to dce7939f86)