nestjs/nest · error · Error

No consumer initialized. Please, call the "connect" method f

Error message

No consumer initialized. Please, call the "connect" method first.

What it means

The `ClientKafka.consumer` getter throws when `_consumer` is null — i.e. before `connect()` has resolved, after `close()`, or whenever the client is running in `producerOnlyMode` (where no consumer is allocated). Guards any consumer-side API access.

Source

Thrown at packages/microservices/client/client-kafka.ts:77

  extends ClientProxy<never, KafkaStatus>
  implements ClientKafkaProxy
{
  protected logger = new Logger(ClientKafka.name);
  protected client: Kafka | null = null;
  protected parser: KafkaParser | null = null;
  protected initialized: Promise<void> | null = null;
  protected responsePatterns: string[] = [];
  protected consumerAssignments: { [key: string]: number } = {};
  protected brokers: string[] | BrokersFunction;
  protected clientId: string;
  protected groupId: string;
  protected producerOnlyMode: boolean;
  protected _consumer: Consumer | null = null;
  protected _producer: Producer | null = null;

  get consumer(): Consumer {
    if (!this._consumer) {
      throw new Error(
        'No consumer initialized. Please, call the "connect" method first.',
      );
    }
    return this._consumer;
  }

  get producer(): Producer {
    if (!this._producer) {
      throw new Error(
        'No producer initialized. Please, call the "connect" method first.',
      );
    }
    return this._producer;
  }

  constructor(protected readonly options: Required<KafkaOptions>['options']) {
    super();

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Await `connect()` before using the consumer (or rely on the framework's `@Client` auto-connect via `onModuleInit`).
  2. If you only produce, set `producerOnlyMode: true` and stop accessing `consumer`.
  3. After `close()`, call `connect()` again before reuse.

Example fix

// before
async onModuleInit() {
  this.client.subscribeToResponseOf('user.created');
  const c = this.client.consumer; // throws
}
// after
async onModuleInit() {
  this.client.subscribeToResponseOf('user.created');
  await this.client.connect();
  const c = this.client.consumer; // ok
}
Defensive patterns

Strategy: validation

Validate before calling

// Ensure connect() resolved before touching the consumer
await this.client.connect();
const assignments = this.client.getConsumerAssignments();
if (Object.keys(assignments).length === 0) { /* consumer not ready */ }

Type guard

function hasConsumer(c: ClientKafka): boolean { return Reflect.get(c, '_consumer') != null; }

Prevention

When it happens

Trigger: Accessing `clientKafka.consumer` before awaiting `connect()`; accessing it in `producerOnlyMode: true`; accessing it after `close()`.

Common situations: Forgetting to `await client.connect()` in `onModuleInit`; calling `commitOffsets`/`subscribeToResponseOf` flow in producer-only mode; race where access precedes connect resolution.

Related errors


AI-assisted analysis of nestjs/nest@6ec0e2783d (2026-08-03). Data as JSON: /data/errors/9a4842a910c93c96.json. Report an issue: GitHub.