Skip to Content

NestJS

Preload one SDK instance and connect it to Nest middleware and the error interceptor.

Installation

npm install server-devtools @nestjs/common @nestjs/core @nestjs/platform-express

Preload initialization

Create server-devtools.js outside src:

const ServerDevTools = require("server-devtools"); const devtools = new ServerDevTools({ auth: { username: process.env.SERVER_DEVTOOLS_USERNAME, password: process.env.SERVER_DEVTOOLS_PASSWORD, }, }); globalThis.__serverDevtools = devtools; void devtools.start();

Enable JavaScript in tsconfig.json:

{ "compilerOptions": { "allowJs": true } }

Mount the shared instance

import { NestFactory } from "@nestjs/core"; import { AppModule } from "./app.module"; import { ServerDevToolsNestInterceptor } from "server-devtools/nestjs"; import ServerDevTools from "server-devtools"; declare global { var __serverDevtools: ServerDevTools | undefined; } async function bootstrap() { const devtools = globalThis.__serverDevtools; if (!devtools) throw new Error("ServerDevTools was not preloaded"); const app = await NestFactory.create(AppModule); app.use((req, res, next) => devtools.middleware(req, res, next)); app.useGlobalInterceptors(new ServerDevToolsNestInterceptor()); await app.listen(3000); } bootstrap();

Start Nest with the preload file:

NODE_OPTIONS='--import ./server-devtools.js' nest start --watch

Run and verify

Open http://localhost:3000/_devtools and request a Nest route. The interceptor records thrown handler errors on the active span; it does not replace Nest’s exception handling.

Troubleshooting

Do not create a second ServerDevTools instance in main.ts. If globalThis.__serverDevtools is undefined, the process was started without NODE_OPTIONS.

Last updated on