mongodb/node-mongodb-native · error · MongoParseError

Invalid `serverApi` property; must specify a version from th

Error message

Invalid `serverApi` property; must specify a version from the following enum: ["${Object.values(ServerApiVersion).join('", "')}"]

What it means

The `serverApi` option requires a version. You may pass a string (treated as `{ version }`) or an object with a `version` field. The transform (src/connection_string.ts:751-772) derives `versionToValidate`; if it is missing or falsy, the driver throws because it cannot pin an API version.

Source

Thrown at src/connection_string.ts:758

  },
  autoSelectFamily: {
    type: 'boolean',
    default: true
  },
  autoSelectFamilyAttemptTimeout: {
    type: 'uint'
  },
  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'
  },

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Set `serverApi: ServerApiVersion.v1` (currently the only supported version)
  2. Or `serverApi: { version: ServerApiVersion.v1 }`
  3. Ensure dynamically-built config always includes a truthy version

Example fix

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

Strategy: validation

Validate before calling

import { ServerApiVersion } from 'mongodb';
function hasServerApiVersion(v) {
  const o = typeof v === 'string' ? { version: v } : v;
  return !!o && typeof o.version === 'string' && o.version.length > 0;
}
if (options.serverApi && !hasServerApiVersion(options.serverApi)) {
  throw new TypeError('serverApi must specify a version');
}

Type guard

import { ServerApiVersion } from 'mongodb';
function isServerApiLike(v) {
  const o = typeof v === 'string' ? { version: v } : v;
  return !!o && typeof o === 'object' && typeof o.version === 'string';
}

Prevention

When it happens

Trigger: `{ serverApi: {} }`; `{ serverApi: { version: undefined } }`; `{ serverApi: { version: '' } }`; `{ serverApi: null }`.

Common situations: Building serverApi dynamically and letting version be undefined; spreading a config object that omits version; conditional config that drops the key.

Related errors


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