express-correlation-id is an Express middleware that provides a unique correlation identifier for each incoming HTTP request, ensuring consistency across all asynchronous operations within the request's lifecycle. It automatically generates a UUID if no `x-correlation-id` header is present (or a configurable custom header). The library is currently stable at version 3.0.1, with recent updates indicating active maintenance. Version 3.x introduced a breaking change by requiring Node.js 16 or newer, with Node.js 20 being recommended. Its key differentiators include a simple API to access the ID via both the `req` object (`req.correlationId()`) and a static module method (`correlator.getId()`), as well as the ability to programmatically set the ID. It differentiates itself by its focus on robust async context handling for the correlation ID.
npm install express-correlation-idVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate the `express-correlation-id` middleware into an Express application, access the generated or provided correlation ID, and programmatically set a new ID for a request.
Upgrade your Node.js environment to version 16 or higher (20 recommended). Alternatively, for older Node.js versions, use `express-correlation-id@2.x`.
Ensure `app.use(correlator())` is called after other relevant middleware, such as `express.json()` or authentication middleware, to guarantee the correlation scope correctly encompasses subsequent handlers.
Always ensure that calls to `correlator.getId()` and `correlator.setId(id)` are made within a request's processing lifecycle, typically within Express middleware or route handlers. For setting the ID, consider using `req.setCorrelationId(id)` as it's scoped to the request object.
Import the default export and access `getId` as a method: `import correlator from 'express-correlation-id'; const id = correlator.getId();`
Ensure `correlator.setId(id)` is only called from within an Express middleware or route handler. If available, use `req.setCorrelationId(id)` as it implicitly operates within the request context.
Use ES Module syntax: `import correlator from 'express-correlation-id';`. Ensure your project's `package.json` has `"type": "module"` or uses `.mjs` file extensions for ESM.
No dependency data recorded yet.