Registry / testing / nestjs-grpc

nestjs-grpc

JSON →
library1.5.2jsnpmunverified

A lightweight, production-ready NestJS package for type-safe gRPC communication between microservices. Current stable version is 1.5.2, released with frequent updates. Key differentiators include a controller-based architecture familiar to NestJS developers, full TypeScript support with code generation from proto files, support for all streaming patterns (unary, server, client, bidirectional), built-in retry logic with exponential backoff, connection pooling, TLS security, and configurable logging. It leverages @nestjs/microservices under the hood but provides a higher-level API with decorators like @GrpcController, @GrpcMethod, and @GrpcStream, along with custom exception classes and CLI tooling for type generation.

npm install nestjs-grpc
INSTALL
IMPORT
SIG · NESTJS-GRPC
N
nestjs-grpc
testingjavascriptv1.5.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GrpcModule
import { GrpcModule } from 'nestjs-grpc'
const GrpcModule = require('nestjs-grpc').GrpcModule
ESM import works in Node >=18. CommonJS require also works but use destructuring.
GrpcController
import { GrpcController } from 'nestjs-grpc'
import { GrpcController } from '@nestjs/common'
@GrpcController is specific to this package, not part of @nestjs/common.
GrpcMethod
import { GrpcMethod } from 'nestjs-grpc'
import { GrpcMethod } from '@nestjs/microservices'
Do not confuse with @nestjs/microservices' GrpcMethod; this package provides its own enhanced version.
GrpcLogLevel
import { GrpcLogLevel } from 'nestjs-grpc'
Enum used for configuring log levels in GrpcModule.forProvider.
GrpcException
import { GrpcException } from 'nestjs-grpc'
Base exception class for gRPC errors; extends RpcException.

Setup a NestJS gRPC server: import GrpcModule with proto config, create a controller with @GrpcController and @GrpcMethod, and use generated types from CLI.

// Install: npm install nestjs-grpc // Generate types from proto files: npx nestjs-grpc generate --proto "./protos/**/*.proto" --output "./src/generated" // app.module.ts import { Module } from '@nestjs/common'; import { GrpcModule, GrpcLogLevel } from 'nestjs-grpc'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; @Module({ imports: [ GrpcModule.forProvider({ protoPath: './protos/auth.proto', package: 'auth', url: '0.0.0.0:50051', logging: { enabled: true, level: GrpcLogLevel.DEBUG, context: 'GrpcModule', }, }), ], controllers: [AuthController], providers: [AuthService], }) export class AppModule {} // auth.controller.ts import { Injectable } from '@nestjs/common'; import { Observable } from 'rxjs'; import { GrpcController, GrpcMethod, GrpcStream, GrpcException } from 'nestjs-grpc'; import { ValidateTokenRequest, ValidateTokenResponse, LoginRequest, LoginResponse, StreamUsersRequest, User } from './generated/auth'; @GrpcController('AuthService') export class AuthController { constructor(private readonly authService: AuthService) {} @GrpcMethod('ValidateToken') async validateToken(request: ValidateTokenRequest): Promise<ValidateTokenResponse> { const user = await this.authService.findByToken(request.token); if (!user) { throw new GrpcException('Invalid token', 16); // UNAUTHENTICATED } return { valid: true, user }; } @GrpcMethod('Login') async login(request: LoginRequest): Promise<LoginResponse> { const user = await this.authService.validateCredentials(request.email, request.password); if (!user) { throw new GrpcException('Invalid credentials', 7); // PERMISSION_DENIED } const token = await this.authService.generateToken(user); return { token, user }; } @GrpcStream('StreamUsers') streamUsers(request: StreamUsersRequest): Observable<User> { return this.authService.getUsersStream(request.limit); } }
Debug
Known issues
breakingIn version 1.0.0, the decorator names changed. @GrpcService was renamed to @GrpcController and @GrpcHandler was split into @GrpcMethod and @GrpcStream.
fix
Update imports: replace @GrpcService with @GrpcController, @GrpcHandler with @GrpcMethod or @GrpcStream accordingly.
affects: <1.0.0
gotchaThe 'package' option in GrpcModule.forProvider must match the proto package name exactly, including case. Mismatches cause runtime 'Method not found' errors.
fix
Ensure the string passed to 'package' matches the 'package' declaration in your .proto file.
affects: >=0.0.0
deprecatedThe forRootAsync method was deprecated in v1.5.0 in favor of dynamic module configuration using forProvider.
fix
Replace GrpcModule.forRootAsync with GrpcModule.forProvider and use the options object directly.
affects: >=1.5.0
gotchaWhen using client-side gRPC, ensure the proto file and package are identical between client and server. Differences can cause silent failures or deserialization errors.
fix
Share the same proto files across services, or use a protobuf registry.
affects: >=0.0.0
breakingIn v1.2.0, the generate CLI command changed its output structure. Generated types now include interfaces with different field naming (snake_case to camelCase conversion default).
fix
Regenerate types with npx nestjs-grpc generate and adjust imports accordingly.
affects: >=1.2.0
Errors
Common errors & fixes
Error: Method not found
The gRPC method name in @GrpcMethod doesn't match the proto definition's RPC method name.
fix
Check that the string passed to @GrpcMethod ('ValidateToken') exactly matches the RPC name in the proto file (rpc ValidateToken).
Error: 12 UNIMPLEMENTED: Method not implemented
The controller does not have a handler for a method defined in the proto service, or the handler method's name is incorrect.
fix
Ensure every RPC method in the proto has a corresponding @GrpcMethod or @GrpcStream handler in the controller class.
Error: ENOENT: no such file or directory, open './protos/auth.proto'
The protoPath in GrpcModule.forProvider points to a non-existent file or the path is resolved incorrectly.
fix
Use an absolute path or ensure the working directory is set correctly. Example: protoPath: resolve(__dirname, '../protos/auth.proto').
TypeError: Cannot read properties of undefined (reading 'name')
The @GrpcController decorator is missing the service name argument, or the decorator is applied to a non-class (e.g., a function).
fix
Apply @GrpcController('ServiceName') to a class and ensure the service name string matches the proto service name.
Upgrade
Version history
1.5.2latest on npm
Audit
Dependencies
@nestjs/commonrequiredPeer dependency for NestJS modules and decorators
@nestjs/corerequiredPeer dependency for NestJS core functionality
@nestjs/microservicesrequiredPeer dependency for gRPC transport layer
reflect-metadatarequiredPeer dependency required for decorators and DI
rxjsrequiredPeer dependency for reactive streams and Observable support
Agent activity
4 hits · last 30 days
node
4
Resources
nestjs-grpc — npm install nestjs-grpc · libregistry