mongodb/node-mongodb-native · error · MongoParseError

Invalid server API version=${versionToValidate}; must be in

Error message

Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(ServerApiVersion).join('", "')}"]

What it means

`serverApi.version` must be one of the `ServerApiVersion` enum values (currently only the string '1'). A version was supplied but is not a recognized value, so the driver refuses to pin an unsupported Stable API version.

Source

Thrown at src/connection_string.ts:765

  },
  bsonRegExp: {
    type: 'boolean'
  },
  serverApi: {
    target: 'serverApi',
    transform({ values: [version] }): ServerApi {
      const serverApiToValidate =
        typeof version === 'string' ? ({ version } as ServerApi) : (version as ServerApi);
      const versionToValidate = serverApiToValidate && serverApiToValidate.version;
      if (!versionToValidate) {
        throw new MongoParseError(
          `Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values(
            ServerApiVersion
          ).join('", "')}"]`
        );
      }
      if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) {
        throw new MongoParseError(
          `Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values(
            ServerApiVersion
          ).join('", "')}"]`
        );
      }
      return serverApiToValidate;
    }
  },
  checkKeys: {
    type: 'boolean'
  },
  compressors: {
    default: 'none',
    target: 'compressors',
    transform({ values }) {
      const compressionList = new Set();
      for (const compVal of values as (CompressorName[] | string)[]) {
        const compValArray = typeof compVal === 'string' ? compVal.split(',') : compVal;

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Use the `ServerApiVersion.v1` constant (the string '1')
  2. Import { ServerApiVersion } from 'mongodb' and reference the enum rather than hardcoding strings
  3. Do not pass numbers — version must be a string

Example fix

// before
new MongoClient(uri, { serverApi: { version: 'v1' } });
// after
import { ServerApiVersion } from 'mongodb';
new MongoClient(uri, { serverApi: { version: ServerApiVersion.v1 } });
Defensive patterns

Strategy: validation

Validate before calling

import { ServerApiVersion } from 'mongodb';
const VALID = new Set(Object.values(ServerApiVersion));
function isValidServerApiVersion(v) {
  const version = typeof v === 'string' ? v : v?.version;
  return VALID.has(version);
}
if (options.serverApi && !isValidServerApiVersion(options.serverApi)) {
  throw new TypeError('Invalid serverApi version');
}

Type guard

import { ServerApiVersion } from 'mongodb';
function isServerApiVersion(v) {
  return Object.values(ServerApiVersion).includes(typeof v === 'string' ? v : v?.version);
}

Prevention

When it happens

Trigger: `{ serverApi: '2' }`; `{ serverApi: { version: 'v1' } }` (must be '1', not 'v1'); `{ serverApi: { version: 1 } }` (number, not the string '1'); `{ serverApi: 'latest' }`.

Common situations: Guessing version strings; using 'v1' instead of '1'; passing a number; assuming 'latest' or a future version string works.

Related errors


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