{"id":18745,"library":"request-context","title":"request-context","description":"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.","status":"maintenance","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/michaelkrone/request-context","tags":["javascript","connect","express","request","context","data","store","middleware","domain"],"install":[{"cmd":"npm install request-context","lang":"bash","label":"npm"},{"cmd":"yarn add request-context","lang":"bash","label":"yarn"},{"cmd":"pnpm add request-context","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package does not ship ESM; use CommonJS require.","wrong":"import contextService from 'request-context';","symbol":"default (contextService)","correct":"const contextService = require('request-context');"},{"note":"Both are valid; destructuring is idiomatic.","wrong":"const middleware = require('request-context').middleware;","symbol":"middleware","correct":"const { middleware } = require('request-context');"},{"note":"Keys must contain a colon separator to identify the namespace, e.g. 'request:user'.","wrong":"contextService.set('key', value);","symbol":"set","correct":"contextService.set('namespace:key', value);"},{"note":"Keys must include the namespace prefix with colon.","wrong":"const val = contextService.get('key');","symbol":"get","correct":"const val = contextService.get('namespace:key');"}],"quickstart":{"code":"const express = require('express');\nconst contextService = require('request-context');\n\nconst app = express();\napp.use(contextService.middleware('request'));\n\napp.use((req, res, next) => {\n  contextService.set('request:user', { name: 'Alice' });\n  next();\n});\n\napp.get('/', (req, res) => {\n  const user = contextService.get('request:user.name');\n  res.send(`Hello ${user}`);\n});\n\napp.use((err, req, res, next) => {\n  res.status(500).send('Error');\n});\n\napp.listen(3000);","lang":"javascript","description":"Sets up Express with request-context middleware, sets a user object, and retrieves nested property via dot notation in route handler."},"warnings":[{"fix":"Migrate to AsyncLocalStorage (Node.js 12.17+) or continuation-local-storage.","message":"Node.js 'domain' module is pending deprecation and may be removed in future versions.","severity":"deprecated","affected_versions":"all"},{"fix":"Always attach an error-handling middleware (Express) or use domain error handlers.","message":"Context is bound to a domain, not the request directly; errors in async callbacks may cause the domain to crash if not handled.","severity":"gotcha","affected_versions":"all"},{"fix":"Use format 'namespace:key' for all set/get calls.","message":"Namespace is required in set/get keys; omitting colon causes undefined behavior.","severity":"breaking","affected_versions":"all"},{"fix":"Use set with object path like 'namespace:obj.key' to set nested property, not set('namespace:obj', { key: val }).","message":"set() with dot notation in value key does not extend existing object; it overwrites the whole property.","severity":"gotcha","affected_versions":"all"},{"fix":"Use set() and get() directly.","message":"setContext and getContext are aliases for set and get; may be removed in future.","severity":"deprecated","affected_versions":"<=2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure app.use(contextService.middleware('request')) is called before any set/get calls, and use the matching namespace prefix.","cause":"Attempting to get a context value without setting namespace middleware or wrong namespace name.","error":"TypeError: Cannot read properties of undefined (reading 'user')"},{"fix":"Add Express error-handling middleware (4 arguments) or a domain error listener to prevent crashes.","cause":"Domain error handler not defined, causing unhandled error to crash the domain and emit on uncaughtException.","error":"Error: Can't set headers after they are sent."},{"fix":"Add const contextService = require('request-context'); at top of file.","cause":"Missing require('request-context') or variable not in scope.","error":"ReferenceError: contextService is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}