mongodb/node-mongodb-native · error · MongoParseError
Invalid `serverApi` property; must specify a version from…
Error message
Invalid `serverApi` property; must specify a version from the following enum: ["${Object.values(ServerApiVersion).join('", "')}"] What it means
Thrown by MongoParseError at src/connection_string.ts:758 in the `serverApi` transform when the supplied value has no `version` field. The driver requires a `version` matching the `ServerApiVersion` enum (currently only `'1'`). The message lists the accepted enum values. Triggered whether you pass a string, an object, or `undefined` for version.
Solutions
- Always include `version`: `{ serverApi: { version: ServerApiVersion.v1, strict: true, deprecationErrors: true } }`.
- Or pass the bare version string `{ serverApi: ServerApiVersion.v1 }` which the driver wraps as `{ version }`.
- Import `ServerApiVersion` from 'mongodb' rather than hard-coding the string.
Example fix
// before
new MongoClient(uri, { serverApi: { strict: true, deprecationErrors: true } })
// after
import { ServerApiVersion } from 'mongodb'
new MongoClient(uri, { serverApi: { version: ServerApiVersion.v1, strict: true, deprecationErrors: true } }) Defensive patterns
Strategy: validation
Validate before calling
function assertServerApiHasVersion(serverApi) { if (!serverApi) return; const v = typeof serverApi === 'string' ? serverApi : serverApi.version; if (!v) throw new Error('serverApi.version is required.'); } Type guard
import { ServerApiVersion } from 'mongodb';
function hasServerApiVersion(v): boolean { if (v == null) return false; if (typeof v === 'string') return true; return typeof v === 'object' && typeof v.version === 'string'; } Try / catch
try { new MongoClient(uri, { serverApi }); } catch (e) { if (e instanceof MongoParseError && /Invalid `serverApi` property/.test(e.message)) { serverApi = { version: ServerApiVersion.v1, ...serverApi }; } else throw e; } Prevention
- Always include `version` when constructing the serverApi object.
- Use the bare-string form `{ serverApi: ServerApiVersion.v1 }` for the simplest case.
When it happens
Trigger: `{ serverApi: {} }`, `{ serverApi: { strict: true } }` (missing version), or `{ serverApi: '' }`.
Common situations: Wanting to enable strict mode and passing only `{ strict: true }`; reading serverApi from config where the version key is absent.
Related errors
- Invalid server API version=
- authMechanism one of
- must be an object
- AuthMechanismProperties must be an object
- Can only specify both of proxy username/password or neither
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/90ac6d92b07e2876.
Report an issue: GitHub.
Appendix: 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 dce7939f86)