Docs
Installation

Installation

This document will guide you through the installation and setting up of NestJS tRPC v2 in your NestJS project.

ℹ️

NestJS tRPC v2 is a maintained fork of the original nestjs-trpc library. This fork provides active maintenance, updated dependencies, and modern tooling while maintaining API compatibility with the original library.

⚠️

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 v2 with your preferred package manager, you can use any of the following commands:

npm install nestjs-trpc-v2 zod @trpc/server

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-v2';
 
@Module({
  imports: [
    TRPCModule.forRoot({
      autoSchemaFile: './src/@generated',
    }),
  ],
})
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. For example, if you want to disable the schema file generation (when deploying to production), you can omit the autoSchemaFile option.

Start your NestJS server. You should be good to go! 🎉

When setting up NestJS tRPC you must set your "sourceMap": true in your compilerOptions within the tsconfig.json configuration file.

Module Options

You can import TRPCModuleOptions type from nestjs-trpc-v2 to safely assert all of the TRPCModule option types.

import { TRPCModule, TRPCModuleOptions } from 'nestjs-trpc-v2';
const trpcOptions: TRPCModuleOptions = {
  autoSchemaFile: './src/@generated',
};
PropTypeDefault
basePath
string
"/trpc"
autoSchemaFile
string
-
schemaFileImports
Array<Function | Object | ZodAny>
-
context
TRPCContext
-
transformer
unknown
-
errorFormatter
(opts: { shape, error }) => {}
-