Docs
Installation

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-trpc

Initialization

Once the packages are installed, we can import the TRPCModule and configure it with the forRoot() static method.

app.module.ts
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 generate

This scans your routers and creates a TypeScript file with your complete router types. For watch mode during development:

npx nestjs-trpc watch

By 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.ts

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',
};
PropTypeDefault
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:

app.module.ts
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)