Registry / http-networking / request-context

request-context

JSON →
library2.0.0jsnpmunverified

Connect/Express middleware that wraps each request in a Node.js domain, providing a namespace to store and retrieve data across async callbacks within the same request lifecycle. Version 2.0.0 is the latest stable release. The package uses the deprecated Node.js 'domain' module, so it is not recommended for new projects. Alternatives include AsyncLocalStorage (Node.js 12.17+) or continuation-local-storage. It supports a simple key-value API with dot notation for nested property access.

npm install request-context
INSTALL
IMPORT
SIG · REQUEST-CONTEXT
R
request-context
http-networkingjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (contextService)
const contextService = require('request-context');
import contextService from 'request-context';
This package does not ship ESM; use CommonJS require.
middleware
const { middleware } = require('request-context');
const middleware = require('request-context').middleware;
Both are valid; destructuring is idiomatic.
set
contextService.set('namespace:key', value);
contextService.set('key', value);
Keys must contain a colon separator to identify the namespace, e.g. 'request:user'.
get
const val = contextService.get('namespace:key');
const val = contextService.get('key');
Keys must include the namespace prefix with colon.

Sets up Express with request-context middleware, sets a user object, and retrieves nested property via dot notation in route handler.

const express = require('express'); const contextService = require('request-context'); const app = express(); app.use(contextService.middleware('request')); app.use((req, res, next) => { contextService.set('request:user', { name: 'Alice' }); next(); }); app.get('/', (req, res) => { const user = contextService.get('request:user.name'); res.send(`Hello ${user}`); }); app.use((err, req, res, next) => { res.status(500).send('Error'); }); app.listen(3000);
Debug
Known issues
deprecatedNode.js 'domain' module is pending deprecation and may be removed in future versions.
fix
Migrate to AsyncLocalStorage (Node.js 12.17+) or continuation-local-storage.
affects: all
gotchaContext is bound to a domain, not the request directly; errors in async callbacks may cause the domain to crash if not handled.
fix
Always attach an error-handling middleware (Express) or use domain error handlers.
affects: all
breakingNamespace is required in set/get keys; omitting colon causes undefined behavior.
fix
Use format 'namespace:key' for all set/get calls.
affects: all
gotchaset() with dot notation in value key does not extend existing object; it overwrites the whole property.
fix
Use set with object path like 'namespace:obj.key' to set nested property, not set('namespace:obj', { key: val }).
affects: all
deprecatedsetContext and getContext are aliases for set and get; may be removed in future.
fix
Use set() and get() directly.
affects: <=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'user')
Attempting to get a context value without setting namespace middleware or wrong namespace name.
fix
Ensure app.use(contextService.middleware('request')) is called before any set/get calls, and use the matching namespace prefix.
Error: Can't set headers after they are sent.
Domain error handler not defined, causing unhandled error to crash the domain and emit on uncaughtException.
fix
Add Express error-handling middleware (4 arguments) or a domain error listener to prevent crashes.
ReferenceError: contextService is not defined
Missing require('request-context') or variable not in scope.
fix
Add const contextService = require('request-context'); at top of file.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
8
Resources
request-context — npm install request-context · libregistry