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

request-context-node

v1.0.1

Published

Async request context utilities for Node.js using AsyncLocalStorage

Readme

request-context

A minimal, production-oriented request context helper for Node.js using AsyncLocalStorage.

Problem Statement

In Node.js backends, request-scoped data like requestId, userId, and correlationId is frequently lost across async boundaries. You might log in one function and handle the request in another, but without a shared context the data is gone. This makes logging, tracing, and debugging far harder than it should be. request-context provides a tiny, typed context that stays attached to the async chain so your logs and handlers stay correlated.

Quick Start

Install:

npm i request-context-node
import { createContext } from "request-context";

type Ctx = { requestId: string; userId?: string };
const ctx = createContext<Ctx>();

await ctx.run({ requestId: "req-123" }, async () => {
  ctx.set("userId", "user-42");
  console.log(ctx.get("requestId"));
});

Express Example

import express from "express";
import { createContext, expressMiddleware } from "request-context";

type Ctx = { requestId: string; userId?: string };
const ctx = createContext<Ctx>();

const app = express();
app.use(
  expressMiddleware(ctx, {
    headerName: "x-request-id",
    getUserId: (req) => req.user?.id
  })
);

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

with() Example

await ctx.run({ requestId: "req-123" }, async () => {
  await ctx.with({ userId: "user-42" }, async () => {
    // requestId + userId available here
  });
  // userId is not set here
});

bind() Example

const bound = ctx.run({ requestId: "req-123" }, () => {
  return ctx.bind(async () => {
    return ctx.get("requestId");
  });
});

await bound(); // "req-123"

Strict vs Non-Strict Mode

By default (strict: false), get returns undefined and set is a no-op when there is no active context. This is safer for libraries and background jobs where a context might legitimately be missing. If you want to enforce correct usage, enable strict mode to throw on missing context.

const ctx = createContext<Ctx>({ strict: true });

Limitations

  • AsyncLocalStorage can lose context in some edge cases (for example, if an async boundary is not tracked by Node's async hooks).
  • This is not distributed tracing. It only tracks context in-process.
  • Cross-process or cross-service propagation is out of scope.

Why not OpenTelemetry?

OpenTelemetry is a full observability framework that includes tracing, metrics, and exporters. This package is intentionally smaller: it only provides a lightweight request context for app-level metadata. If you need end-to-end tracing or vendor integrations, OpenTelemetry is the right tool.