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-mqtt.ts:296

      this._status$.next(MqttStatus.DISCONNECTED);
    });
  }

  public registerCloseListener(client: MqttClient) {
    client.on(MqttEventsMap.CLOSE, () => {
      this._status$.next(MqttStatus.CLOSED);
    });
  }

  public registerConnectListener(client: MqttClient) {
    client.on(MqttEventsMap.CONNECT, () => {
      this._status$.next(MqttStatus.CONNECTED);
    });
  }

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

  public on<
    EventKey extends keyof MqttEvents = keyof MqttEvents,
    EventCallback extends MqttEvents[EventKey] = MqttEvents[EventKey],
  >(event: EventKey, callback: EventCallback) {
    if (this.mqttClient) {
      this.mqttClient.on(event, callback as any);
    } else {
      this.pendingEventListeners.push({ event, callback });
    }
  }

  protected initializeSerializer(options: MqttOptions['options']) {

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Call app.listen() or app.startAllMicroservices() before accessing the MQTT server instance.
  2. Await listen() so the MQTT connection is established before use.
  3. Move any code accessing the server to run after bootstrap listening completes.

Example fix

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

When it happens

Trigger: Thrown at packages/microservices/server/server-mqtt.ts:296 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/46a02e3970b14850.json. Report an issue: GitHub.