Skip to Content
đź‘‹ Hey there! Welcome to NestJS tRPC.
DocumentationConfiguration

Configuration

Learn how to configure NestJS tRPC for your application.

Basic Module Setup

Create a TRPCModule in your NestJS application:

import { Module } from '@nestjs/common'
import { TRPCModule } from '@nexica/nestjs-trpc'
import { RequestContext, RequestContextFactory } from './context/app.context'
import { UserModule } from './routers/user/user.module'
 
@Module({
    imports: [
        UserModule,
        TRPCModule.forRoot<RequestContext>({
            transformer: 'superjson', // Optional: for serializing dates, maps, sets, etc.
            outputPath: './src/generated/server.ts', // Schema output location
            context: RequestContextFactory<RequestContext>,
            basePath: '/trpc', // tRPC endpoint prefix - defaults to /trpc
            generateSchemas: true, // Enable schema generation - defaults to true
        }),
    ],
    providers: [RequestContextFactory<RequestContext>],
})
export class TrpcModule {}

Configuration Options

Core Options

OptionTypeRequiredDescriptionDefault
contextTRPCContextYesContext factory for request handling
outputPathstringNoOutput path for generated schema./../generated/server.ts
basePathstringNoAPI endpoint prefix/trpc
transformer'superjson' | 'devalue'NoData transformer
driver'express' | 'fastify'NoPlatform driverauto-detected
generateSchemasbooleanNoEnable schema generationtrue

Advanced Options

OptionTypeRequiredDescriptionDefault
injectFilesstring[]NoGlob pattern for including files
websocketWebSocketOptionsNoWebSocket configuration

WebSocket Options

OptionTypeRequiredDescriptionDefault
enabledbooleanNoEnable WebSocket supportfalse
portnumberNoWebSocket server port (creates new WSS)
pathstringNoWebSocket endpoint path/trpc
wssWebSocketServerNoExisting WebSocketServer instance

Example Configuration

TRPCModule.forRoot({
    // Core configuration
    context: RequestContextFactory,
    outputPath: './src/generated/server.ts',
    basePath: '/trpc',
    generateSchemas: true, // Enable schema generation
 
    // Data transformation
    transformer: 'superjson', // or 'devalue'
 
    // File injection for schema generation
    injectFiles: ['@/zod/**/*'], // Glob pattern for including files
 
    // WebSocket configuration
    websocket: {
        enabled: true,
        port: 4001, // WebSocket server port
        path: '/trpc', // WebSocket path
        // Or provide existing WebSocket server:
        // wss: existingWebSocketServer
    },
 
    // Driver selection
    driver: 'express', // or 'fastify' (auto-detected if not specified)
})

Note: For Fastify applications, WebSocket connections automatically include keepAlive settings (pingMs: 30000, pongWaitMs: 5000).

Data Transformers

NestJS tRPC automatically handles transformer imports and configuration. Simply specify the transformer name:

TRPCModule.forRoot({
    transformer: 'superjson', // Handles dates, maps, sets, etc.
    // ... other options
})

Using Devalue

TRPCModule.forRoot({
    transformer: 'devalue', // Alternative serialization
    // ... other options
})

Note: The library automatically imports and configures the specified transformer. For SuperJSON, it imports the default export. For Devalue, it configures { serialize: stringify, deserialize: parse }.

Next Steps

With your module configured, you’re ready to:

Last updated on: