{"id":"f50e6e4e9867bdfb","repo":"nestjs/nest","slug":"no-consumer-initialized","errorCode":null,"errorMessage":"No consumer initialized","messagePattern":"No consumer initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-kafka.ts","lineNumber":279,"sourceCode":"    }\n    const source = defer(async () => this.connect()).pipe(\n      mergeMap(() => this.dispatchBatchEvent({ pattern, data })),\n    );\n    const connectableSource = connectable(source, {\n      connector: () => new Subject(),\n      resetOnDisconnect: false,\n    });\n    connectableSource.connect();\n    return connectableSource;\n  }\n\n  public commitOffsets(\n    topicPartitions: TopicPartitionOffsetAndMetadata[],\n  ): Promise<void> {\n    if (this._consumer) {\n      return this._consumer.commitOffsets(topicPartitions);\n    } else {\n      throw new Error('No consumer initialized');\n    }\n  }\n\n  public unwrap<T>(): T {\n    if (!this.client) {\n      throw new Error(\n        'Not initialized. Please call the \"connect\" method first.',\n      );\n    }\n    return this.client as T;\n  }\n\n  public on<\n    EventKey extends string | number | symbol = string | number | symbol,\n    EventCallback = any,\n  >(event: EventKey, callback: EventCallback) {\n    throw new Error('Method is not supported for Kafka client');\n  }","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-kafka.ts#L261-L297","documentation":"`ClientKafka.commitOffsets()` throws a bare `Error('No consumer initialized')` when `_consumer` is null — before `connect()` completes or in `producerOnlyMode`. Manual offset commits require a live consumer; this guard prevents a null dereference deeper in kafkajs.","triggerScenarios":"Calling `clientKafka.commitOffsets([{ topic, partition, offset }])` before `connect()` resolves, or while running with `producerOnlyMode: true`.","commonSituations":"At-least-once consumer trying to commit offsets manually before the consumer has joined the group; producer-only client reused for offset management.","solutions":["Await `connect()` before calling `commitOffsets`.","Disable `producerOnlyMode` if you need a consumer for manual commits.","Gate manual commits behind a `getConsumerAssignments()` readiness check."],"exampleFix":"// before\nawait this.client.commitOffsets([{ topic: 'users', partition: 0, offset: '42' }]); // throws\n// after\nawait this.client.connect();\nawait this.client.commitOffsets([{ topic: 'users', partition: 0, offset: '42' }]);","handlingStrategy":"validation","validationCode":"// Only commit offsets once a consumer exists and has joined\nawait this.client.connect();\nconst assignments = this.client.getConsumerAssignments();\nif (Object.keys(assignments).length > 0) {\n  await this.client.commitOffsets([{ topic: 'users', partition: 0, offset: '42' }]);\n}","typeGuard":"function hasConsumer(c: ClientKafka): boolean { return Reflect.get(c, '_consumer') != null; }","tryCatchPattern":"try {\n  await this.client.commitOffsets(offsets);\n} catch (e) {\n  if (e instanceof Error && /No consumer initialized/.test(e.message)) { /* defer retry */ }\n  throw e;\n}","preventionTips":["Await `connect()` before manual offset commits.","Don't run manual commits in `producerOnlyMode`.","Check `getConsumerAssignments()` to confirm the group has joined before committing."],"tags":["kafka","microservices","lifecycle","consumer","nestjs"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}