express-metrics-middleware
raw JSON → 0.1.14 verified Sat Apr 25 auth: no javascript
Express middleware that automatically collects standard Prometheus metrics (HTTP request duration, request count, active connections, etc.) for Node.js applications. Version 0.1.14 is the current stable release, updated infrequently. Compared to prom-client or express-prom-bundle, this package offers a zero-configuration setup with sensible defaults and built-in TypeScript types. It is opinionated and designed for quick integration without manual metric definition, but may have limited customization options.
Common errors
error TypeError: createMiddleware is not a function ↓
cause Using default import instead of named import.
fix
Use import { createMiddleware } from 'express-metrics-middleware'
error Cannot find module 'prom-client' or its corresponding type declarations. ↓
cause Missing peer dependency prom-client.
fix
Run npm install prom-client
error Metrics endpoint returns 404 ↓
cause Middleware not registered or route conflict.
fix
Ensure app.use(createMiddleware()) is called before other routes.
Warnings
gotcha The middleware exposes a /metrics endpoint by default; ensure it does not conflict with your routes or middleware order (place it before authentication if metrics should be public). ↓
fix Route /metrics explicitly with your own handler if you need custom behavior.
breaking In version 0.1.0, the function was named 'metricsMiddleware' and exported as default. In 0.1.1+ it changed to named export 'createMiddleware'. ↓
fix Use import { createMiddleware } from 'express-metrics-middleware' instead.
gotcha The middleware relies on prom-client's default metrics (collectDefaultMetrics) which may increase memory usage in long-lived processes. Disable if not needed via options. ↓
fix Pass { collectDefaultMetrics: false } to createMiddleware.
Install
npm install express-metrics-middleware yarn add express-metrics-middleware pnpm add express-metrics-middleware Imports
- createMiddleware wrong
import createMiddleware from 'express-metrics-middleware'correctimport { createMiddleware } from 'express-metrics-middleware' - MetricsConfig
import type { MetricsConfig } from 'express-metrics-middleware' - createMiddleware (require) wrong
const createMiddleware = require('express-metrics-middleware')correctconst { createMiddleware } = require('express-metrics-middleware')
Quickstart
import express from 'express';
import { createMiddleware } from 'express-metrics-middleware';
const app = express();
app.use(createMiddleware({ metricPrefix: 'my_app' }));
app.get('/hello', (req, res) => {
res.send('hello world');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
// Metrics endpoint is automatically exposed at /metrics by default