Installation
This document will guide you through the installation and setting up of NestJS tRPC in your nestjs project.
If you don't have a NestJS project setup yet, please visit our NestJS Quickstart Guide or check out the official NestJS documentation.
Manual Installation
To install NestJS tRPC with your preferred package manager, you can use any of the following commands:
npm install nestjs-trpcInitialization
Once the packages are installed, we can import the TRPCModule and configure it with the forRoot() static method.
import { Module } from '@nestjs/common';
import { TRPCModule } from 'nestjs-trpc';
@Module({
imports: [
TRPCModule.forRoot({
// Optional: specify custom base path (default: '/trpc')
// basePath: '/api/trpc',
}),
],
})
export class AppModule {}The forRoot() method takes an options object as an argument. These options are passed through to the underlying express or fastify driver.
Generate Types
Run the CLI to generate your AppRouter types:
npx nestjs-trpc generateThis scans your routers and creates a TypeScript file with your complete router types. For watch mode during development:
npx nestjs-trpc watchBy default, types are generated to ./src/@generated/server.ts. You can customize this with the --output flag:
npx nestjs-trpc generate --output ./src/trpc/types.tsFor projects using "moduleResolution": "NodeNext" or "Node16" in tsconfig.json, .js
extensions are added automatically. The CLI searches the nearest tsconfig.json walking up
from your source directory, so it works correctly whether you run from the project root or a
subdirectory. Override with --import-extension js|none|auto:
npx nestjs-trpc generate --import-extension noneNote: If your project uses moduleResolution: bundler (e.g. with Vite or esbuild) but
runs on plain Node ESM at runtime, auto will report false because bundler mode does not
require explicit file extensions at compile time. Use --import-extension=js explicitly in
that case.
Start your NestJS server. You should be good to go! 🎉
Module Options
You can import TRPCModuleOptions type from nestjs-trpc to safely assert all of the TRPCModule option types.
import { TRPCModule, TRPCModuleOptions } from 'nestjs-trpc';
const trpcOptions: TRPCModuleOptions = {
basePath: '/trpc',
};| Prop | Type | Default |
|---|---|---|
basePath | string | "/trpc" |
context | TRPCContext | - |
transformer | unknown | - |
errorFormatter | (opts: { shape, error }) => {} | - |
logger | LoggerService | ConsoleLogger |
onError | TRPCErrorHandler | - |
globalMiddlewares | Array<TRPCMiddleware> | [] |
Custom Logger
By default, NestJS tRPC uses the built-in NestJS ConsoleLogger. You can provide your own logger by passing any implementation of the NestJS LoggerService interface:
import { Module } from '@nestjs/common';
import { TRPCModule } from 'nestjs-trpc';
import { MyLogger } from './my-logger.service';
@Module({
imports: [
TRPCModule.forRoot({
logger: new MyLogger(),
}),
],
})
export class AppModule {}This is useful when replacing the default NestJS logger with a production-grade alternative such as Pino (opens in a new tab), Winston (opens in a new tab), or any other logger that implements the LoggerService interface.
Supported Platforms
The CLI binary is available for:
- macOS (Intel and Apple Silicon)
- Linux (x64 and ARM64)
- Windows (x64)