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

The `SrvPoller` constructor requires an options object containing a `srvHost` string (srv_polling.ts:54). SrvPoller is `@internal` and performs periodic DNS SRV record lookups for `mongodb+srv://` URIs to discover cluster hosts. Without a srvHost it cannot build the SRV query name.

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 3366c21a63)

Solutions

  1. Use a well-formed `mongodb+srv://cluster.example.net/dbname` URI with a real SRV hostname.
  2. If not using SRV discovery, use a standard `mongodb://host:port` URI instead.
  3. Update the driver and report the URI if the error persists with a valid-looking SRV host.

Example fix

// before
const client = new MongoClient('mongodb+srv:///mydb');
// after
const client = new MongoClient('mongodb+srv://cluster.example.net/mydb');
Defensive patterns

Strategy: validation

Validate before calling

function assertSrvUri(uri) {
  if (uri.startsWith('mongodb+srv://') && uri.replace('mongodb+srv://', '').split('/')[0] === '') {
    throw new TypeError('mongodb+srv URI must include a host');
  }
}

Type guard

function hasSrvHost(uri) {
  return !uri.startsWith('mongodb+srv://') || /mongodb\+srv:\/\/[^/]+/.test(uri);
}

Prevention

When it happens

Trigger: Internal construction of SrvPoller with missing options or a srvHost that is empty/undefined; a `mongodb+srv://` URI whose host portion parsed to empty; misconfigured client options reaching the poller.

Common situations: Malformed SRV connection string (`mongodb+srv:///dbname` with no host); programmatic client construction without a host; a bug in URI parsing on older driver versions.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/f6e1adf34e8bcdcc.json. Report an issue: GitHub.