mongodb/node-mongodb-native · error · MongoRuntimeError

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

Error message

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

What it means

Thrown by the `Topology` constructor while building the seedlist: each seed must be either a string or a `HostAddress` instance (topology.ts:274). Any other type (number, plain object without host info, null element) cannot be turned into a server address.

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

Solutions

  1. Pass seeds as `host:port` strings, or wrap them with `HostAddress.fromString(...)`.
  2. Filter/map your seedlist to strings before passing it to MongoClient.
  3. Use the standard connection-string URI form to avoid manual seed construction.

Example fix

// before
new MongoClient([{ host: 'localhost', port: 27017 }]);
// after
new MongoClient('mongodb://localhost:27017');
Defensive patterns

Strategy: validation

Validate before calling

function normalizeSeeds(seeds) {
  return seeds.map(s => typeof s === 'string' ? HostAddress.fromString(s) : s)
    .filter(s => s instanceof HostAddress);
}

Type guard

function isHostSeed(s) {
  return typeof s === 'string' || s instanceof HostAddress;
}

Prevention

When it happens

Trigger: Passing MongoClient a seedlist containing a non-string/non-HostAddress entry, e.g. `new MongoClient([{ host: 'x' }])` or a numeric port-only entry; an object that is not a HostAddress.

Common situations: Constructing a seedlist dynamically and including a raw config object; migrating code that previously accepted looser seed shapes; a deserialized JSON array whose objects lost their HostAddress type.

Related errors


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