Application Insights Express Middleware
raw JSON → 0.0.5 verified Fri May 01 auth: no javascript deprecated
A package that monkey-patches Express and the Application Insights SDK for Node.js to automatically track Express middleware execution as dependency telemetry. It provides a setAutoCollectExpressMiddleware() method to enable collection, sending trackDependency() calls with middleware names and durations. Current version 0.0.5, last updated in May 2020. This is an early-stage project with no production guarantee; it is a WIP and likely has bugs. It targets the deprecated Azure Application Insights SDK and is not maintained actively. Consider the official @opentelemetry/instrumentation-express and @azure/monitor-opentelemetry for modern use cases.
Common errors
error TypeError: appInsights.setAutoCollectExpressMiddleware is not a function ↓
cause The middleware package was not required (or required after applicationinsights), so the method was not added to appInsights object.
fix
Ensure you require('applicationinsights-express-middleware') before const appInsights = require('applicationinsights').
error Error: Cannot find module 'applicationinsights-express-middleware' ↓
cause Package not installed or missing from node_modules.
fix
Run npm install applicationinsights-express-middleware.
error Cannot read properties of undefined (reading 'trackDependency') ↓
cause Application Insights not started or not configured properly.
fix
Call appInsights.setup('key').start() after requiring the middleware package.
Warnings
deprecated Project is abandoned; last release in 2020. Uses deprecated Azure Application Insights SDK. ↓
fix Migrate to @opentelemetry/instrumentation-express and @azure/monitor-opentelemetry.
breaking v0.0.5 renamed 'wrap' to 'patch' internally; API unchanged but internal monkey-patching changed. ↓
fix Update your code to use the new internal names if you relied on undocumented internals.
gotcha Must require this package before both applicationinsights and express. ↓
fix Reorder requires: require('applicationinsights-express-middleware') first, then applicationinsights, then express.
gotcha No support for dispose(); cannot stop instrumentation once started. ↓
fix Restart the application to stop tracking.
deprecated Azure Application Insights SDK is deprecated in favor of OpenTelemetry-based SDK. ↓
fix Use @azure/monitor-opentelemetry with @opentelemetry/instrumentation-express instead.
Install
npm install applicationinsights-express-middleware yarn add applicationinsights-express-middleware pnpm add applicationinsights-express-middleware Imports
- default wrong
import applicationinsightsExpress from 'applicationinsights-express-middleware'correctrequire('applicationinsights-express-middleware') - setAutoCollectExpressMiddleware wrong
require('applicationinsights-express-middleware').setAutoCollectExpressMiddleware(true)correctappInsights.setup('key'); appInsights.setAutoCollectExpressMiddleware(true); appInsights.start(); - ApplicationInsights wrong
import { ApplicationInsights } from 'applicationinsights'correctconst appInsights = require('applicationinsights');
Quickstart
// run: npm install applicationinsights-express-middleware applicationinsights express
require('applicationinsights-express-middleware');
const appInsights = require('applicationinsights');
const express = require('express');
const app = express();
appInsights
.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY ?? '')
.setAutoCollectExpressMiddleware(true)
.start();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000, () => console.log('Listening on 3000'));