DEV Community

Cover image for NestJS Profiler πŸš€βš‘οΈ
Mohamed Raslan
Mohamed Raslan

Posted on

NestJS Profiler πŸš€βš‘οΈ

Stop flying blind in your ππžπ¬π­π‰π’ apps! πŸš€

One thing I always missed from the π‹πšπ«πšπ―πžπ₯/π’π²π¦πŸπ¨π§π² world was the Web Profiler.

Being able to see every database query, request duration, and cache hit in real-time is a game-changer for debugging. πŸͺ›

I couldn't find a perfect equivalent for NestJS... 𝒔𝒐 𝑰 π’ƒπ’–π’Šπ’π’• π’Šπ’•. πŸš€

Introducing 𝐧𝐞𝐬𝐭𝐣𝐬-𝐩𝐫𝐨𝐟𝐒π₯𝐞𝐫 β€” a lightweight, web-based dashboard for your NestJS backend.

βœ… Inspect HTTP request with hell of details about it.
βœ… Track executed DB queries with analyze (PostgreSQL, MySQL, MongoDB).
βœ… Monitor cache operations.
βœ… Listing all the entities.
βœ… Listing all available routes.
βœ… Listing all logs from the terminal.
βœ… Web UI built right into your app.

Installation

npm install nestjs-profiler
Enter fullscreen mode Exit fullscreen mode

Peer Dependencies (Optional)

Install the dependencies relevant to your project:

# For PostgreSQL
npm install pg

# For MongoDB
npm install mongodb

# For MySQL
npm install mysql2

# For Cache Profiling
npm install @nestjs/cache-manager cache-manager
Enter fullscreen mode Exit fullscreen mode

Configuration

Import ProfilerModule in your root AppModule.

import { Module } from '@nestjs/common';
import { ProfilerModule } from 'nestjs-profiler';

@Module({
  imports: [
    ProfilerModule.forRoot({
      // Global enable/disable (default: true)
      enabled: process.env.NODE_ENV !== 'production',

      // PostgreSQL Profiling
      collectQueries: true,
      explain: {
        enabled: true,
        thresholdMs: 50, // Only explain queries taking > 50ms
        analyze: false, // If true, runs EXPLAIN ANALYZE (execution!)
      },
      // Add pgDriver
      pgDriver: pg,

      // Add mysql2 driver for MySQL profiling
      mysqlDriver: mysql2,

      // Add mongodb driver for MongoDB profiling
      mongoDriver: mongodb,

      // MongoDB Profiling (default: true)
      collectMongo: true,

      // MySQL Profiling (default: true)
      collectMysql: true,

      // Cache Profiling (default: true)
      collectCache: true,

      // Log Profiling (default: true)
      collectLogs: true,

      // Storage backend (default: InMemory)
      // You can implement custom storage by passing an object implementing ProfilerStorage
      storage: 'memory',
    }),
  ],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Usage

1. Initialize Explorers (Optional but Recommended)

To enable the Entity Explorer and Route Explorer features in the dashboard, initialize the profiler in your main.ts file right after creating the application.

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ProfilerModule } from 'nestjs-profiler';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Initialize Profiler Explorers
  ProfilerModule.initialize(app);

  await app.listen(3000);
}
bootstrap();
Enter fullscreen mode Exit fullscreen mode

2. Accessing the Dashboard

Once configured, start your application. The profiler automatically intercepts requests and queries.

Navigate to http://localhost:3000/__profiler (or your app's port).

3. JSON API

You can also retrieve profile data programmatically:

  • GET /__profiler/json - List recent requests
  • GET /__profiler/:id/json - Get details for a specific request

NOTE: IMPORTANT

  • The profiler is designed for development and debugging. Do not enable in production due to performance overhead and potential security risks.
  • If you have a prefix for your API routes (e.g., /api), you have to adjust the global prefix like this:
  app.setGlobalPrefix('api', {
    exclude: [{ path: '__profiler/(.*)', method: RequestMethod.ALL }],
  });
Enter fullscreen mode Exit fullscreen mode

I'd love to hear your feedback! What's the one feature you wish NestJS had out of the box?

Npm package link: nestjs-profiler

Github: NestJS-Profiler-Repo

Top comments (0)