nestjs/nest · error · Error

Not initialized. Please call the "listen"/"startAllMicroserv

Error message

Not initialized. Please call the "listen"/"startAllMicroservices" method before accessing the server.

What it means

Error "Not initialized. Please call the "listen"/"startAllMicroservices" method before accessing the server." thrown in nestjs/nest.

Source

Thrown at packages/microservices/server/server-kafka.ts:262

    return this.onProcessingStartHook(
      this.transportId,
      kafkaContext,
      async () => {
        const response$ = this.transformToObservable(
          handler(packet.data, kafkaContext),
        );

        const replayStream$ = new ReplaySubject();
        await this.combineStreamsAndThrowIfRetriable(response$, replayStream$);

        this.send(replayStream$, publish);
      },
    );
  }

  public unwrap<T>(): T {
    if (!this.client) {
      throw new Error(
        'Not initialized. Please call the "listen"/"startAllMicroservices" method before accessing the server.',
      );
    }
    return [this.client, this.consumer, this.producer] as T;
  }

  public on<
    EventKey extends string | number | symbol = string | number | symbol,
    EventCallback = any,
  >(event: EventKey, callback: EventCallback) {
    throw new Error('Method is not supported for Kafka server');
  }

  private combineStreamsAndThrowIfRetriable(
    response$: Observable<any>,
    replayStream$: ReplaySubject<unknown>,
  ) {
    return new Promise<void>((resolve, reject) => {

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Call app.listen() (or app.startAllMicroservices()) before accessing the underlying Kafka server/client, consumer, or producer.
  2. Await the listen promise so initialization completes before any access.
  3. Check initialization order: access this.client/consumer/producer only inside code that runs after bootstrap listening has started.

Example fix

const app = await NestFactory.createMicroservice(AppModule, opts);
await app.listen();
// now safe to access the server instance
const kafka = app.get(ServerKafka);

When it happens

Trigger: Thrown at packages/microservices/server/server-kafka.ts:262 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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