mongodb/node-mongodb-native · critical · MongoInvalidArgumentError
No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} de
Error message
No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined. What it means
Thrown in prepareHandshakeDocument when the user's mechanism is the default 'MONGODB-CR' placeholder and the code attempts to pre-fetch the SCRAM-SHA-256 provider, but getOrCreateProvider returns undefined. The inline comment states 'This auth mechanism is always present', so reaching this throw means the default provider registry has been corrupted or overridden - this should never happen in a normal install.
Source
Thrown at src/cmap/connect.ts:266
compression: compressors
};
if (options.loadBalanced === true) {
handshakeDoc.loadBalanced = true;
}
const credentials = authContext.credentials;
if (credentials) {
if (credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && credentials.username) {
handshakeDoc.saslSupportedMechs = `${credentials.source}.${credentials.username}`;
const provider = authContext.options.authProviders.getOrCreateProvider(
AuthMechanism.MONGODB_SCRAM_SHA256,
credentials.mechanismProperties
);
if (!provider) {
// This auth mechanism is always present.
throw new MongoInvalidArgumentError(
`No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined.`
);
}
return await provider.prepare(handshakeDoc, authContext);
}
const provider = authContext.options.authProviders.getOrCreateProvider(
credentials.mechanism,
credentials.mechanismProperties
);
if (!provider) {
throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`);
}
return await provider.prepare(handshakeDoc, authContext);
}
return handshakeDoc;
}
/**View on GitHub (pinned to 3366c21a63)
Solutions
- Remove any custom authProviders option from MongoClientOptions so the default registry is used.
- Reinstall node_modules (rm -rf node_modules && npm install) to rule out a corrupted install.
- If using a bundler (webpack/esbuild), ensure the driver's auth provider modules are not tree-shaken or marked external incorrectly.
- Upgrade to a current driver release; if it persists, file a driver bug.
Example fix
// before
const client = new MongoClient(uri, { authProviders: customRegistryMissingScram });
// after
const client = new MongoClient(uri); // default registry always has SCRAM-SHA-256 Defensive patterns
Strategy: validation
Validate before calling
import { MongoClient, AuthMechanism } from 'mongodb';
// Smoke test that the default SCRAM-SHA-256 provider loads
function smokeTestProvider() {
const c = new MongoClient('mongodb://u:p@localhost');
const prov = c.options.authProviders?.getOrCreateProvider?.(AuthMechanism.MONGODB_SCRAM_SHA256);
if (!prov) throw new Error('Driver install is missing the SCRAM-SHA-256 provider');
} Type guard
function installIsHealthy(authProviders: any): boolean {
return Boolean(authProviders?.getOrCreateProvider('SCRAM-SHA-256'));
} Try / catch
import { MongoInvalidArgumentError } from 'mongodb';
try {
await client.connect();
} catch (e) {
if (e instanceof MongoInvalidArgumentError && /SCRAM-SHA-256/.test(e.message)) {
// reinstall node_modules or remove custom authProviders override
}
throw e;
} Prevention
- Never strip the SCRAM-SHA-256 provider from a custom authProviders registry.
- Pin the driver version and lock node_modules to avoid partial installs.
- If bundling, mark 'mongodb' and its auth provider subpaths as external.
When it happens
Trigger: Only when a custom authProviders was supplied to MongoClient that omits the SCRAM-SHA-256 provider, or the driver's module graph was tampered with such that the SCRAM-SHA-256 provider module failed to load. Fires in prepareHandshakeDocument (src/cmap/connect.ts:260-269) before the handshake is even sent.
Common situations: A custom authProviders option that filters out ScramSha256; a broken tree-shake/bundler configuration that dropped the default provider modules; a fork of the driver with the SCRAM provider registration removed; running against a corrupted node_modules.
Related errors
- Node.js crypto module is required for SCRAM-SHA-1 authentica
- No AuthProvider for ${resolvedCredentials.mechanism} defined
- No AuthProvider for ${credentials.mechanism} defined.
- Username required for mechanism '${this.mechanism}'
- username and ENVIRONMENT '${this.mechanismProperties.ENVIRON
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/56f2ddd91a1d9e3f.json.
Report an issue: GitHub.