Flamegraph Middleware provides an HTTP interface for on-demand CPU and heap profiling of Node.js applications, visualizing the results as interactive flamegraphs directly in the browser. It is currently at stable version 1.0.0 and designed to be framework-agnostic, working seamlessly with native `http` servers, Express, Fastify, and other HTTP frameworks. Key differentiators include its non-blocking background profiling, dual CPU and heap profiling capabilities, and temporary in-memory storage of profiles with automatic expiration. This tool is invaluable for identifying performance bottlenecks and memory leaks in production or development environments, offering a user-friendly WebGL-powered visualization for deep analysis of execution paths and memory allocation patterns. The package emphasizes ease of integration and real-time insights.
npm install flamegraph-middlewareVerified import paths — ran on the pinned version, not inferred.
Demonstrates integrating `flamegraph-middleware` into an Express application, showing how to configure it, start a CPU/heap profile via HTTP, and access the interactive flamegraph visualizations.
Ensure your project is configured for ESM (`"type": "module"` in `package.json`) and you are running Node.js 18+ or higher. Use `import` statements exclusively.
Register `@fastify/middie` before using `flamegraph-middleware`: `await fastify.register(middie); fastify.use(createFlamegraphMiddleware(...))`
Carefully manage `defaultDuration` and `maxDuration` in production environments. Request profiles with specific durations (e.g., `?duration=5000`) and avoid excessively long profiles.
Adjust `maxProfiles` and `profileTTL` configuration options based on your monitoring needs and available memory. Download or view profiles promptly after completion if long-term storage is required.
Switch your project to ESM by adding `"type": "module"` to your `package.json` and using `import { createFlamegraphMiddleware } from 'flamegraph-middleware'`.Ensure you are using the *result* of the factory function. For Express/Fastify, it should be `app.use(createFlamegraphMiddleware())` or `app.use(flamegraphMiddlewareInstance)`.
Replace `require('module')` with `import module from 'module'` or `import { namedExport } from 'module'` for all dependencies.