{"id":"9348cf6deeb542fd","repo":"nestjs/nest","slug":"no-producer-initialized-please-call-the-connect","errorCode":null,"errorMessage":"No producer initialized. Please, call the \"connect\" method first.","messagePattern":"No producer initialized\\. Please, call the \"connect\" method first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-kafka.ts","lineNumber":86,"sourceCode":"  protected brokers: string[] | BrokersFunction;\n  protected clientId: string;\n  protected groupId: string;\n  protected producerOnlyMode: boolean;\n  protected _consumer: Consumer | null = null;\n  protected _producer: Producer | null = null;\n\n  get consumer(): Consumer {\n    if (!this._consumer) {\n      throw new Error(\n        'No consumer initialized. Please, call the \"connect\" method first.',\n      );\n    }\n    return this._consumer;\n  }\n\n  get producer(): Producer {\n    if (!this._producer) {\n      throw new Error(\n        'No producer initialized. Please, call the \"connect\" method first.',\n      );\n    }\n    return this._producer;\n  }\n\n  constructor(protected readonly options: Required<KafkaOptions>['options']) {\n    super();\n\n    const clientOptions = this.getOptionsProp(\n      this.options,\n      'client',\n      {} as KafkaConfig,\n    );\n    const consumerOptions = this.getOptionsProp(\n      this.options,\n      'consumer',\n      {} as ConsumerConfig,","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-kafka.ts#L68-L104","documentation":"The `ClientKafka.producer` getter throws when `_producer` is null — i.e. before `connect()` has resolved or after `close()` has been called. All produce-side operations route through this getter, so it is the first thing to fail when the client is used before being connected.","triggerScenarios":"Accessing `clientKafka.producer` (directly or via `emit`/`send`) before `connect()` resolves, or after `close()`.","commonSituations":"Missing `await client.connect()` in `onModuleInit`; race condition where `emit` is called before the connect promise resolves; reusing a client after close without reconnecting.","solutions":["Await `connect()` (or let the framework auto-connect via `@Client`) before producing.","Ensure no `emit`/`send` races the connect promise; chain off `connect().then(...)` if needed.","Call `connect()` again after `close()` before reusing the client."],"exampleFix":"// before\nasync onModuleInit() {\n  this.client.emit('user.created', { id: 1 }); // throws via producer getter\n}\n// after\nasync onModuleInit() {\n  await this.client.connect();\n  this.client.emit('user.created', { id: 1 });\n}","handlingStrategy":"validation","validationCode":"// Ensure connect() resolved before producing\nawait this.client.connect();\nthis.client.emit('user.created', { id: 1 });","typeGuard":"function hasProducer(c: ClientKafka): boolean { return Reflect.get(c, '_producer') != null; }","tryCatchPattern":null,"preventionTips":["Await `connect()` before any `emit`/`send`.","Chain produce calls off `connect().then(...)` to avoid races.","Reconnect after `close()` before reusing the client."],"tags":["kafka","microservices","lifecycle","client-proxy","nestjs"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}