Monitoring Node.js Applications: Tools and Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Monitoring JavaScript (Node.js) Applications: Tools and Libraries



Monitoring Node.js applications is a critical task for maintaining the stability, performance, and availability of services. Without constant metric collection, error tracking, and code profiling, you risk encountering unexpected crashes, slow performance, and memory leaks. In this article, we will review the best tools and libraries for monitoring Node.js, including popular open-source solutions.



Why is Node.js monitoring needed?

Node.js is an asynchronous environment that handles I/O operations well but has features requiring attention:

  • Single-threaded model: blocking code can stop the entire process.
  • Memory leaks: due to closures and improper reference management.
  • Event Loop: its lag directly affects response speed.
  • Async code errors: unhandled promises can go unnoticed.


Main categories of tools

For comprehensive monitoring of a Node.js application, you will need tools from three categories:

  • Metrics Collection: CPU, memory, requests per second, response time.
  • Profiling: analysis of bottlenecks and the event loop.
  • Error Tracking: automatic collection and aggregation of exceptions.


1. Prometheus + prom-client (Metrics Collection)

Prometheus is the de facto standard for metric collection in modern infrastructure. For Node.js, the prom-client library is used. It allows creating histograms, counters, and gauge metrics.



Installation:

npm install prom-client express


Example of exporting metrics (server.js):

const express = require('express');const client = require('prom-client');const app = express();

// Create a histogram for response timeconst httpRequestDurationMicroseconds = new client.Histogram({ name: 'http_request_duration_ms', help: 'Duration of HTTP requests in ms', labelNames: ['method', 'route', 'status'], buckets: [50, 100, 200, 500, 1000, 2000]});

// Middleware for measuring timeapp.use((req, res, next) => { const end = httpRequestDurationMicroseconds.startTimer(); res.on('finish', () => { end({ method: req.method, route: req.route?.path || 'unknown', status: res.statusCode }); }); next();});

// Endpoint for Prometheusapp.get('/metrics', async (req, res) => { res.set('Content-Type', client.register.contentType); res.end(await client.register.metrics());});

app.get('/', (req, res) => res.send('Hello World!'));app.listen(3000, () => console.log('Server running on port 3000'));


Now you can configure Prometheus to collect metrics from /metrics and visualize them in Grafana.



2. PM2 (Process Manager and Basic Monitoring)

PM2 is not only a process manager for Node.js but also a built-in monitoring system. It allows you to track process status, restart them on crash, and collect basic metrics.



Installation:

npm install pm2 -g


Starting an application with monitoring:

pm2 start server.js --name "my-app"pm2 monit  # Open monitoring dashboard


Exporting metrics to Prometheus (via pm2-metrics):

npm install pm2-metricspm2 start server.js --name "my-app"pm2-metrics --port 9200  # Opens /metrics endpoint


3. Clinic.js (Profiling and Diagnostics)

Clinic.js is a set of tools

Recommendations