mongodb/node-mongodb-native · error · MongoParseError
Cannot use srvMaxHosts or srvServiceName with a non-srv…
Error message
Cannot use srvMaxHosts or srvServiceName with a non-srv connection string
What it means
Thrown by MongoParseError (else branch at src/connection_string.ts:494) when the URI is plain `mongodb://` (not +srv) but the user supplied `srvMaxHosts` or `srvServiceName`. Those options only have meaning for SRV record resolution, so a non-SRV connection has nothing for them to do.
Solutions
- Switch the URI scheme to `mongodb+srv://` if you want SRV-record-driven discovery.
- Otherwise remove `srvMaxHosts` and `srvServiceName` from both the URI query string and the options object.
Example fix
// before
new MongoClient('mongodb://host1:27017,host2:27017/?srvMaxHosts=2')
// after (plain mongodb, no srv option)
new MongoClient('mongodb://host1:27017,host2:27017/') Defensive patterns
Strategy: validation
Validate before calling
function assertSrvOptionsMatchScheme(uri, options = {}) {
const isSrv = uri.startsWith('mongodb+srv://');
if (!isSrv && (options.srvMaxHosts != null || options.srvServiceName || /[?&]srv(MaxHosts|ServiceName)=/.test(uri))) {
throw new Error('srvMaxHosts / srvServiceName require a mongodb+srv:// URI.');
}
} Try / catch
try { new MongoClient(uri, options); } catch (e) { if (e instanceof MongoParseError && /srvMaxHosts or srvServiceName with a non-srv/.test(e.message)) { delete options.srvMaxHosts; delete options.srvServiceName; } else throw e; } Prevention
- When switching between +srv and plain mongodb:// URIs, scrub srvMaxHosts and srvServiceName from the config.
- Keep a single source of truth for the URI scheme so dependent options can be derived from it.
When it happens
Trigger: `mongodb://host1:27017,host2:27017/?srvMaxHosts=2`, or `mongodb://host:27017/?srvServiceName=_tcp.example.com`, or passing either option via the options object with a plain mongodb:// URI.
Common situations: Switching a URI from `mongodb+srv://` to `mongodb://` but forgetting to drop the srv-only options; sharing an options object across srv and non-srv clients.
Related errors
- Cannot limit srv hosts with loadBalanced enabled
- Cannot use srvMaxHosts option with replicaSet
- SRV URI does not support directConnection
- Cannot combine replicaSet option with srvMaxHosts
- Cannot have empty URI params in DNS TXT Record
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/f6c55f83fb5dbf71.
Report an issue: GitHub.
Appendix: 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 dce7939f86)