{"id":"56f2ddd91a1d9e3f","repo":"mongodb/node-mongodb-native","slug":"no-authprovider-for-authmechanism-mongodb-scram","errorCode":null,"errorMessage":"No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined.","messagePattern":"No AuthProvider for (.+?) defined\\.","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"critical","filePath":"src/cmap/connect.ts","lineNumber":266,"sourceCode":"    compression: compressors\n  };\n\n  if (options.loadBalanced === true) {\n    handshakeDoc.loadBalanced = true;\n  }\n\n  const credentials = authContext.credentials;\n  if (credentials) {\n    if (credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && credentials.username) {\n      handshakeDoc.saslSupportedMechs = `${credentials.source}.${credentials.username}`;\n\n      const provider = authContext.options.authProviders.getOrCreateProvider(\n        AuthMechanism.MONGODB_SCRAM_SHA256,\n        credentials.mechanismProperties\n      );\n      if (!provider) {\n        // This auth mechanism is always present.\n        throw new MongoInvalidArgumentError(\n          `No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined.`\n        );\n      }\n      return await provider.prepare(handshakeDoc, authContext);\n    }\n    const provider = authContext.options.authProviders.getOrCreateProvider(\n      credentials.mechanism,\n      credentials.mechanismProperties\n    );\n    if (!provider) {\n      throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`);\n    }\n    return await provider.prepare(handshakeDoc, authContext);\n  }\n  return handshakeDoc;\n}\n\n/**","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/connect.ts#L248-L284","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst client = new MongoClient(uri, { authProviders: customRegistryMissingScram });\n\n// after\nconst client = new MongoClient(uri); // default registry always has SCRAM-SHA-256","handlingStrategy":"validation","validationCode":"import { MongoClient, AuthMechanism } from 'mongodb';\n// Smoke test that the default SCRAM-SHA-256 provider loads\nfunction smokeTestProvider() {\n  const c = new MongoClient('mongodb://u:p@localhost');\n  const prov = c.options.authProviders?.getOrCreateProvider?.(AuthMechanism.MONGODB_SCRAM_SHA256);\n  if (!prov) throw new Error('Driver install is missing the SCRAM-SHA-256 provider');\n}","typeGuard":"function installIsHealthy(authProviders: any): boolean {\n  return Boolean(authProviders?.getOrCreateProvider('SCRAM-SHA-256'));\n}","tryCatchPattern":"import { MongoInvalidArgumentError } from 'mongodb';\ntry {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /SCRAM-SHA-256/.test(e.message)) {\n    // reinstall node_modules or remove custom authProviders override\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["auth","auth-provider","scram","configuration","runtime"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}