{"id":"7b536f5cfd2df1ed","repo":"mongodb/node-mongodb-native","slug":"clientsession-requires-a-mongoclient","errorCode":null,"errorMessage":"ClientSession requires a MongoClient","messagePattern":"ClientSession requires a MongoClient","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sessions.ts","lineNumber":168,"sourceCode":"   * Create a client session.\n   * @internal\n   * @param client - The current client\n   * @param sessionPool - The server session pool (Internal Class)\n   * @param options - Optional settings\n   * @param clientOptions - Optional settings provided when creating a MongoClient\n   */\n  constructor(\n    client: MongoClient,\n    sessionPool: ServerSessionPool,\n    options: ClientSessionOptions,\n    clientOptions: MongoOptions\n  ) {\n    super();\n    this.on('error', noop);\n\n    if (client == null) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError('ClientSession requires a MongoClient');\n    }\n\n    if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError('ClientSession requires a ServerSessionPool');\n    }\n\n    options = options ?? {};\n\n    this.snapshotEnabled = options.snapshot === true;\n    if (options.causalConsistency === true && this.snapshotEnabled) {\n      throw new MongoInvalidArgumentError(\n        'Properties \"causalConsistency\" and \"snapshot\" are mutually exclusive'\n      );\n    }\n\n    this.client = client;\n    this.sessionPool = sessionPool;","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sessions.ts#L150-L186","documentation":"The `ClientSession` constructor requires a non-null MongoClient (sessions.ts:167). ClientSession is an `@internal`-ish construct; application code should obtain sessions exclusively through `client.startSession()`, which guarantees a client is present.","triggerScenarios":"Directly instantiating `new ClientSession(undefined, pool, ...)`; a custom subclass or test double that drops the client argument; an internal refactor passing null.","commonSituations":"Attempting to construct sessions manually instead of via the client; unit tests that stub ClientSession without a real client.","solutions":["Always create sessions with `const session = client.startSession(options)`.","In tests, use a real (possibly in-memory/mocked) MongoClient rather than constructing ClientSession directly.","Do not subclass or re-wrap ClientSession without forwarding the client."],"exampleFix":"// before\nconst session = new ClientSession(undefined, pool, {});\n// after\nconst session = client.startSession();","handlingStrategy":"validation","validationCode":"function startSession(client, options) {\n  if (!client) throw new TypeError('ClientSession requires a MongoClient');\n  return client.startSession(options);\n}","typeGuard":"function isMongoClient(c) {\n  return c != null && typeof c.startSession === 'function';\n}","tryCatchPattern":null,"preventionTips":["Never construct ClientSession directly; always use client.startSession().","In tests, use a real or properly mocked MongoClient.","Forward the client argument in any session wrappers."],"tags":["sessions","internal"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}