mongodb/node-mongodb-native · error · MongoParseError
Cannot use srvMaxHosts or srvServiceName with a non-srv conn
Error message
Cannot use srvMaxHosts or srvServiceName with a non-srv connection string
What it means
Thrown in the non-SRV branch (connection_string.ts:486-498) when a plain mongodb:// URI is given but srvMaxHosts or srvServiceName appears in either the URI query string or the options object. These options only make sense for mongodb+srv:// URIs because they parameterize the SRV DNS lookup.
Source
Thrown at src/connection_string.ts:494
if (mongoOptions.srvMaxHosts > 0 && typeof mongoOptions.replicaSet === 'string') {
throw new MongoParseError('Cannot use srvMaxHosts option with replicaSet');
}
// SRV turns on TLS by default, but users can override and turn it off
const noUserSpecifiedTLS = !objectOptions.has('tls') && !urlOptions.has('tls');
const noUserSpecifiedSSL = !objectOptions.has('ssl') && !urlOptions.has('ssl');
if (noUserSpecifiedTLS && noUserSpecifiedSSL) {
mongoOptions.tls = true;
}
} else {
const userSpecifiedSrvOptions =
urlOptions.has('srvMaxHosts') ||
objectOptions.has('srvMaxHosts') ||
urlOptions.has('srvServiceName') ||
objectOptions.has('srvServiceName');
if (userSpecifiedSrvOptions) {
throw new MongoParseError(
'Cannot use srvMaxHosts or srvServiceName with a non-srv connection string'
);
}
}
if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {
throw new MongoParseError('directConnection option requires exactly one host');
}
if (
!mongoOptions.proxyHost &&
(mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)
) {
throw new MongoParseError('Must specify proxyHost if other proxy options are passed');
}
if (
(mongoOptions.proxyUsername && !mongoOptions.proxyPassword) ||View on GitHub (pinned to 3366c21a63)
Solutions
- Remove srvMaxHosts and srvServiceName from URI and options.
- Or change the URI scheme to mongodb+srv:// if you genuinely want SRV resolution.
Example fix
// before
const c = new MongoClient('mongodb://host:27017/?srvMaxHosts=3');
// after
const c = new MongoClient('mongodb://host:27017/');
// or
const c = new MongoClient('mongodb+srv://cluster.example/?srvMaxHosts=3'); Defensive patterns
Strategy: validation
Validate before calling
if (!uri.startsWith('mongodb+srv://')) {
const params = new URL(uri).searchParams;
if (params.has('srvMaxHosts') || params.has('srvServiceName') || 'srvMaxHosts' in opts || 'srvServiceName' in opts) {
throw new Error('srvMaxHosts/srvServiceName require a mongodb+srv:// URI');
}
} Prevention
- Keep SRV-only options grouped with mongodb+srv:// URIs in config.
- Add a CI test that asserts option/scheme pairing.
When it happens
Trigger: URI like 'mongodb://host:27017/?srvMaxHosts=3' or new MongoClient('mongodb://host:27017/', { srvServiceName: 'customservice' }).
Common situations: Switching a URI from mongodb+srv:// to mongodb:// for testing or firewalls but leaving the SRV-only options in place; templated config that injects srv options unconditionally.
Related errors
- SRV URI does not support directConnection
- Option "srvHost" must not be empty
- No addresses found at host
- Multiple text records not allowed
- Text record may only set any of: ${VALID_TXT_RECORDS.join(',
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/f6c55f83fb5dbf71.json.
Report an issue: GitHub.