express-ruid

raw JSON →
1.1.5 verified Sat Apr 25 auth: no javascript

Express.js middleware for generating unique request IDs with prefixes (pid, hostname) and sequence numbers. v1.1.5, stable. Uses built-in crypto. Supports HTTP header inheritance, configurable attribute names, and optional express-http-context integration. Lighter than express-request-id, no external dependencies.

error TypeError: ruid is not a function
cause Using ES import with require or wrong default export import.
fix
Use import ruid from 'express-ruid' or const ruid = require('express-ruid').default if using ESM require.
error Cannot read properties of undefined (reading 'ruid')
cause Accessing req.ruid before the middleware runs or missing ruid() call.
fix
Ensure app.use(ruid()) is placed before route handlers.
error express-http-context middleware not used
cause setInContext true but httpContext.middleware not added.
fix
Add app.use(httpContext.middleware) before ruid.
error Property 'ruid' does not exist on type 'Request'
cause TypeScript lacks declaration; package ships types but not augmented correctly.
fix
Install @types/express and ensure tsconfig includes express-ruid types.
breaking v1.1.0 removed app.locals support and introduced express-http-context dependency for context storage.
fix Use setInContext option and add httpContext.middleware if you need context access. For app.locals, use setInLocals: true (added back in v1.2.0?) – check docs.
deprecated setInLocals option deprecated in v1.1.0, but restored in v1.2.0? Actually not, stay on v1.0.x if you rely on app.locals.
fix Upgrade to v1.2.0+ or use setInContext with express-http-context.
gotcha Forgetting to add httpContext.middleware before ruid when setInContext:true leads to context loss.
fix Place app.use(httpContext.middleware) BEFORE app.use(ruid({ setInContext: true })).
gotcha Request ID attribute name changed from 'rid' to 'ruid' in v1.x? Check default: it's 'ruid' in latest.
fix Access req.ruid instead of req.rid.
gotcha Middleware resets sequence counter on server restart? Prefix uses random unique part, but sequence may repeat.
fix Use header-based ID propagation across microservices to ensure uniqueness.
npm install express-ruid
yarn add express-ruid
pnpm add express-ruid

Basic usage: generates unique request ID and attaches to req.ruid.

import express, { Request, Response } from 'express';
import ruid from 'express-ruid';

const app = express();

// Add request ID middleware
app.use(ruid());

app.get('/', (req: Request, res: Response) => {
  console.log(`Request ID: ${req.ruid}`);
  res.json({ ruid: req.ruid });
});

app.listen(3000);
// Output: Request ID: 1234@hostname/abc123-0000000000000001