mongodb/node-mongodb-native · error · MongoRuntimeError

Topology cannot be constructed from

Error message

Topology cannot be constructed from ${JSON.stringify(seed)}

What it means

Thrown by the Topology constructor (src/sdam/topology.ts:276) as MongoRuntimeError when a seed in the seedlist is neither a string nor a HostAddress instance. The constructor accepts a string, HostAddress, or array thereof; any other type (number, object, null) is rejected. Usually indicates a malformed connection string or a programming error passing the wrong object to MongoClient.

Solutions

  1. Pass a valid connection string to MongoClient: new MongoClient('mongodb://host:27017').
  2. If constructing a Topology directly (advanced/internal), pass string or HostAddress seeds only.
  3. Log the seed value to identify the source of the bad input.

Example fix

// before
const client = new MongoClient({ host: 'localhost' });
// after
const client = new MongoClient('mongodb://localhost:27017');
Defensive patterns

Strategy: validation

Validate before calling

function assertConnectionString(uri) {
  if (typeof uri !== 'string') throw new TypeError('connection string must be a string');
}

Type guard

function isConnectionString(v): v is string {
  return typeof v === 'string' && /^mongodb(\+srv)?:\/\/.+/.test(v);
}

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof MongoRuntimeError && /Topology cannot be constructed/.test(e.message)) {
    // log the seed value and fix the caller
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a non-string/non-HostAddress seed to MongoClient or Topology, e.g. new MongoClient(42) or internal code that produced an object seed. Connection-string parsing normally yields strings/HostAddresses, so a non-string seed implies an upstream bug or misuse.

Common situations: Programmatic construction of a MongoClient with the wrong argument type, a custom URI parser that returns objects, or driver internals mis-shaping seeds.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/ae927285473ec515. Report an issue: GitHub.

Appendix: source

Thrown at src/sdam/topology.ts:276

      hosts: [HostAddress.fromString('localhost:27017')],
      ...Object.fromEntries(DEFAULT_OPTIONS.entries())
    };

    if (typeof seeds === 'string') {
      seeds = [HostAddress.fromString(seeds)];
    } else if (!Array.isArray(seeds)) {
      seeds = [seeds];
    }

    const seedlist: HostAddress[] = [];
    for (const seed of seeds) {
      if (typeof seed === 'string') {
        seedlist.push(HostAddress.fromString(seed));
      } else if (seed instanceof HostAddress) {
        seedlist.push(seed);
      } else {
        // FIXME(NODE-3483): May need to be a MongoParseError
        throw new MongoRuntimeError(`Topology cannot be constructed from ${JSON.stringify(seed)}`);
      }
    }

    const topologyType = topologyTypeFromOptions(options);
    const topologyId = globalTopologyCounter++;

    const selectedHosts =
      options.srvMaxHosts == null ||
      options.srvMaxHosts === 0 ||
      options.srvMaxHosts >= seedlist.length
        ? seedlist
        : shuffle(seedlist, options.srvMaxHosts);

    const serverDescriptions = new Map();
    for (const hostAddress of selectedHosts) {
      serverDescriptions.set(hostAddress.toString(), new ServerDescription(hostAddress));
    }

View on GitHub (pinned to dce7939f86)