mongodb/node-mongodb-native · error · MongoRuntimeError
Options for SrvPoller must exist and include srvHost
Error message
Options for SrvPoller must exist and include srvHost
What it means
Thrown by the SrvPoller constructor (src/sdam/srv_polling.ts:55) as MongoRuntimeError when the options object is missing or has no srvHost field. SrvPoller resolves hostnames for mongodb+srv:// connection strings by polling DNS SRV records; it requires a hostname to poll. This is an internal component constructed by the topology during SRV-based connection setup.
Solutions
- Ensure the mongodb+srv:// URI has a valid hostname: mongodb+srv://cluster.example.net/dbname.
- Verify DNS resolution of the SRV hostname works (nslookup or dig).
- If the URI looks correct, report to driver maintainers as an internal error.
Example fix
// before
const client = new MongoClient('mongodb+srv:///test');
// after
const client = new MongoClient('mongodb+srv://cluster.example.net/test'); Defensive patterns
Strategy: validation
Validate before calling
function assertSrvUri(uri) {
if (uri.startsWith('mongodb+srv://')) {
const host = uri.slice('mongodb+srv://'.length).split('/')[0];
if (!host) throw new Error('mongodb+srv URI missing hostname');
}
} Type guard
function hasSrvHost(uri): boolean {
return /^mongodb\+srv:\/\/[^/]+/.test(uri);
} Try / catch
try {
await client.connect();
} catch (e) {
if (e instanceof MongoRuntimeError && /srvHost/.test(e.message)) {
// fix the URI to include a hostname
}
throw e;
} Prevention
- Validate mongodb+srv:// URIs include a hostname before connecting.
- Test SRV hostnames with dig/nslookup in CI.
- Avoid hand-building SRV URIs from templated parts that may drop the host.
When it happens
Trigger: Connecting with mongodb+srv:// but the parsed URI produced no srvHost (e.g. an empty or malformed SRV URI), or internal code constructing SrvPoller without options. Reached only on mongodb+srv:// connections.
Common situations: Malformed mongodb+srv:// URI missing the hostname (e.g. mongodb+srv:///dbname), corrupted URI parsing, or a driver bug.
Related errors
- Option "srvHost" must not be empty
- 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/f6e1adf34e8bcdcc.
Report an issue: GitHub.
Appendix: source
Thrown at src/sdam/srv_polling.ts:55
export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
srvHost: string;
rescanSrvIntervalMS: number;
heartbeatFrequencyMS: number;
haMode: boolean;
generation: number;
srvMaxHosts: number;
srvServiceName: string;
_timeout?: NodeJS.Timeout;
/** @event */
static readonly SRV_RECORD_DISCOVERY = 'srvRecordDiscovery' as const;
constructor(options: SrvPollerOptions) {
super();
this.on('error', noop);
if (!options || !options.srvHost) {
throw new MongoRuntimeError('Options for SrvPoller must exist and include srvHost');
}
this.srvHost = options.srvHost;
this.srvMaxHosts = options.srvMaxHosts ?? 0;
this.srvServiceName = options.srvServiceName ?? 'mongodb';
this.rescanSrvIntervalMS = 60000;
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000;
this.haMode = false;
this.generation = 0;
this._timeout = undefined;
}
get srvAddress(): string {
return `_${this.srvServiceName}._tcp.${this.srvHost}`;
}
View on GitHub (pinned to dce7939f86)