npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-trace-id

v1.0.0

Published

express-trace-id is a lightweight Express middleware that ensures every incoming request has a traceability identifier.

Readme

express-trace-id

npm version npm downloads

Lightweight Express middleware that automatically assigns a unique trace ID to every incoming request. Enables seamless request correlation for debugging, structured logging, and distributed system observability.

Table of Contents

Installation

npm install express-trace-id

Requires Node.js 14.x or higher and Express 4.x or 5.x.

Quick Start

const express = require("express");
const { traceId } = require("express-trace-id");

const app = express();

app.use(traceId());

app.get("/", (req, res) => {
  res.json({ traceId: req.traceId });
});

app.listen(3000);

Every request automatically receives:

  • A unique trace ID available as req.traceId
  • The trace ID in response headers as x-trace-id

Logging Integration

Use getTraceId() to access the trace ID anywhere in your request chain thanks to Node.js AsyncLocalStorage:

const express = require("express");
const { traceId, getTraceId } = require("express-trace-id");

const app = express();
app.use(traceId());

app.get("/users/:id", async (req, res) => {
  // Access via req.traceId
  console.log(`[${req.traceId}][GET /users/:id] Request started`);

  const user = await fetchUser(req.params.id);

  // Access via getTraceId() function
  const traceId = getTraceId();
  console.log(`[${traceId}][GET /users/:id] Request completed`);

  res.json({ user });
});

async function fetchUser(userId) {
  // Access via getTraceId() in async function
  const traceId = getTraceId();
  console.log(`[${traceId}][fetchUser] Fetching user ${userId}`);

  const user = await db.users.findById(userId);

  console.log(`[${traceId}][fetchUser] Found user ${user.name}`);

  return user;
}

app.listen(3000);

Log Output:

[a7f3e2d1-4b8c-4a5e-9f1d-8c7b6a5e4d3c][GET /users/:id] Request started
[a7f3e2d1-4b8c-4a5e-9f1d-8c7b6a5e4d3c][fetchUser] Fetching user 123
[a7f3e2d1-4b8c-4a5e-9f1d-8c7b6a5e4d3c][fetchUser] Found user John Doe
[a7f3e2d1-4b8c-4a5e-9f1d-8c7b6a5e4d3c][GET /users/:id] Request completed

All logs share the same trace ID, making it easy to track a request through your entire application.

API Reference

traceId()

Automatically generates a UUID v4 trace ID when not provided. Available via req.traceId, getTraceId(), or the x-trace-id header.

Usage:

app.use(traceId());

Returns 400 Bad Request if a custom trace ID fails validation.

getTraceId()

Retrieves the current request's trace ID from anywhere in your code.

Returns:

  • string - The trace ID during a request
  • "no-trace-id" - Outside a request context

Example:

function logError(error) {
  console.error(`[${getTraceId()}] Error:`, error.message);
}

X_TRACE_ID

Constant for the trace ID header name: "x-trace-id"

Example:

const { X_TRACE_ID } = require("express-trace-id");

app.get("/custom", (req, res) => {
  res.json({ traceId: req.header(X_TRACE_ID) });
});

Validation

Trace IDs must match: /^[a-zA-Z0-9_-]+$/

Valid:

  • 550e8400-e29b-41d4-a716-446655440000 (auto-generated UUID)
  • request_abc123
  • trace-2024-001

Invalid:

  • invalid@id (special characters)
  • my trace id (spaces)

Custom trace IDs that fail validation return a 400 Bad Request response. For best results, let the middleware auto-generate UUIDs to ensure uniqueness and avoid collisions.

License

ISC

Author: Sion Young