A lightweight utility for managing asynchronous context in Node.js using AsyncLocalStorage. v1.0.4 provides a simple API to create, get, set, and run context across async boundaries, with built-in Express middleware for request-level context. Unlike heavy solutions like continuation-local-storage or cls-hooked, it leverages Node's native AsyncLocalStorage for performance and reliability. Ships TypeScript definitions. Updated infrequently (last release Nov 2023).
npm install node-context-storageNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a typed context, runs an async function within it, and demonstrates get/set/exists methods.
Use context.exists() before calling context.get() to avoid runtime errors.
If you need to replace the entire context, call context.set() with all properties or use a new run().
Check GitHub for updates; consider alternatives like @opentelemetry/api if you need more features.
For worker threads, manually pass context or use a dedicated context propagation library.
Wrap the code inside context.run() or check context.exists() before accessing context.
Provide an initial store with the correct shape: context.run({ context: { userId: '...' } }, ...).Import from the package root: import { createContextMiddleware } from 'node-context-storage'.