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-nats.ts:267

        case 'update':
          this.logger.log(
            `NatsStatus: type: "${status.type}", data: "${data}".`,
          );
          this.statusEventEmitter.emit(NatsEventsMap.UPDATE, status.data);
          break;

        default:
          this.logger.log(
            `NatsStatus: type: "${status.type}", data: "${data}".`,
          );
          break;
      }
    }
  }

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

  public on<
    EventKey extends keyof E = keyof E,
    EventCallback extends E[EventKey] = E[EventKey],
  >(event: EventKey, callback: EventCallback) {
    this.statusEventEmitter.on(event as string | symbol, callback as any);
  }

  protected initializeSerializer(options: NatsOptions['options']) {
    this.serializer = options?.serializer ?? new NatsRecordSerializer();
  }

  protected initializeDeserializer(options: NatsOptions['options']) {

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Call app.listen() or app.startAllMicroservices() before accessing the NATS server instance.
  2. Await the listen promise to guarantee the NATS connection exists before use.

Example fix

const app = await NestFactory.createMicroservice(AppModule, opts);
await app.listen();
const server = app.get(ServerNats);

When it happens

Trigger: Thrown at packages/microservices/server/server-nats.ts:267 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/f691e11a2fb3c1dc.json. Report an issue: GitHub.