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.tsStart your NestJS server. You should be good to go! 🎉
Setting "sourceMap": true in your compilerOptions within tsconfig.json can help with debugging, but is not required for the CLI to work.
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 }) => {} | - |
Supported Platforms
The CLI binary is available for:
- macOS (Intel and Apple Silicon)
- Linux (x64 and ARM64)
- Windows (x64)