Provides a unified API for asynchronous local storage in Node.js, using the native AsyncLocalStorage (available since Node.js 12.17.0 / 14.0.0) with automatic fallback to cls-hooked for older Node.js versions (>=6). Version 1.0.2 is the latest stable release. It offers a simple get/set/runWith interface and is designed to be a drop-in replacement for CLS-based libraries, making it easier to manage request-scoped data without worrying about Node.js version compatibility. Ships TypeScript definitions.
npm install asynchronous-local-storageNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates creating a request-scoped context with Express, setting a default value, then retrieving it in a route handler.
Always wrap your code in runWith before using set/get, or check als.getActiveContext() to see if a context exists.
Update code: replace `require('asynchronous-local-storage')(fn)` with `als.runWith(fn)`.Use runWith instead of manual context creation/destruction.
Use `import als from 'asynchronous-local-storage'` and then `als.runWith(...)` or destructure `{ runWith }`.Ensure you import correctly: `import { als } from 'asynchronous-local-storage'` (type: module) or `const { als } = require('asynchronous-local-storage')`.Make sure you call set/get within the same async chain started by runWith. Consider using AsyncResource to bind callbacks if needed.