{"id":"287c50d85cd44a14","repo":"nestjs/nest","slug":"the-connect-method-is-not-supported-in-grpc-mo","errorCode":null,"errorMessage":"The \"connect()\" method is not supported in gRPC mode.","messagePattern":"The \"connect\\(\\)\" method is not supported in gRPC mode\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-grpc.ts","lineNumber":362,"sourceCode":"        pkg = pkg[name];\n      }\n    }\n\n    return pkg;\n  }\n\n  public close() {\n    this.clients.forEach(client => {\n      if (client && isFunction(client.close)) {\n        client.close();\n      }\n    });\n    this.clients.clear();\n    this.grpcClients = [];\n  }\n\n  public async connect(): Promise<any> {\n    throw new Error('The \"connect()\" method is not supported in gRPC mode.');\n  }\n\n  public send<TResult = any, TInput = any>(\n    pattern: any,\n    data: TInput,\n  ): Observable<TResult> {\n    throw new Error(\n      'Method is not supported in gRPC mode. Use ClientGrpc instead (learn more in the documentation).',\n    );\n  }\n\n  protected getClient(name: string): any {\n    return this.grpcClients.find(client =>\n      Object.hasOwnProperty.call(client, name),\n    );\n  }\n\n  protected publish(packet: any, callback: (packet: any) => any): any {","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-grpc.ts#L344-L380","documentation":"`ClientGrpcProxy.connect()` unconditionally throws because gRPC clients open channels lazily per call via grpc-js and do not support the eager-connect lifecycle used by other transports. Calling `connect()` is a misuse of the `ClientGrpc` API surface.","triggerScenarios":"Calling `await clientGrpc.connect()` explicitly, or generic lifecycle code (e.g. an `onModuleInit` loop) that invokes `connect()` on every `ClientProxy` regardless of transport.","commonSituations":"Code copied from a TCP/Redis/NATS/MQTT example that calls `connect()`; a base class treating all clients uniformly; upgrading a transport from TCP to gRPC without removing the connect call.","solutions":["Remove the explicit `connect()` call — for gRPC you obtain a service via `getService()` and call methods directly.","Branch generic lifecycle code on transport type before calling `connect()`.","Use the `@Client(() => ClientGrpc)` / `ClientsModule` pattern and skip manual connect."],"exampleFix":"// before\nconst client = ClientProxyFactory.create({ transport: Transport.GRPC, options });\nawait client.connect();\n// after\nconst client = ClientProxyFactory.create({ transport: Transport.GRPC, options }) as ClientGrpc;\nconst math = client.getService<IMathService>('Math');\nmath.sum({ data: [1, 2] }).subscribe(...);","handlingStrategy":"type-guard","validationCode":"import { ClientGrpc, ClientProxy } from '@nestjs/microservices';\nfunction supportsConnect(c: ClientProxy | ClientGrpc): boolean {\n  return typeof (c as any).connect === 'function' && c.constructor.name !== 'ClientGrpcProxy';\n}\nif (supportsConnect(client)) await client.connect();","typeGuard":"import { ClientGrpc } from '@nestjs/microservices';\nfunction isGrpcClient(c: unknown): c is ClientGrpc {\n  return typeof c === 'object' && c !== null && typeof (c as any).getService === 'function';\n}","tryCatchPattern":null,"preventionTips":["Do not call `connect()` on gRPC clients — they connect lazily per call.","Branch lifecycle code by transport type.","Use `ClientsModule` / `@Client()` and let the framework manage lifecycle."],"tags":["grpc","microservices","client-proxy","api-misuse","nestjs"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}