Registry / devops / async-local-storage

async-local-storage

JSON →
library2.3.1jsnpmunverified

An async hooks-based thread-local-like storage for Node.js, providing per-call-chain context propagation. Version 2.3.1 supports Node >=8.0.0 using async_hooks. It offers scopable storage, linked top behavior, and create time tracking. Compared to alternatives like cls-hooked or async_hooks directly, it provides a simpler API but lacks built-in automatic cleanup and has known performance overhead. Last updated in 2018; no active maintenance observed.

npm install async-local-storage
INSTALL
IMPORT
SIG · ASYNC-LOCAL-STORAG
A
async-local-storage
devopsjavascriptv2.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
import als from 'async-local-storage'
const als = require('async-local-storage')
CommonJS require works in Node <14; ESM import is preferred for modern code.
enable
import { enable } from 'async-local-storage'
const enable = require('async-local-storage').enable
Named exports are available; require destructuring works but ESM is safer.
scope
import { scope } from 'async-local-storage'
als.scope()
Must be called within a context where async_hooks tracking is active, typically after enable().
set/get
import { set, get } from 'async-local-storage'
als.set(...)
Named import avoids globals; set/get are used like als.set/als.get once imported.

Basic usage: enable async hooks, create a scope, set and get a value within the same call chain.

import als from 'async-local-storage'; als.enable(); als.scope(); als.set('requestId', 'abc123'); setTimeout(() => { const id = als.get('requestId'); console.log('Retrieved in timeout:', id); }, 10); als.disable();
Debug
Known issues
gotchaenable() must be called before using scope/set/get; otherwise operations silently fail.
fix
Call als.enable() once at startup before any async operations.
affects: >=1.0.0
gotchascope() changes the call chain top — parent values are no longer accessible after calling scope().
fix
Use scope() only when you want a fresh context; otherwise skip it.
affects: >=1.0.0
deprecatedasync_hooks used by this package can degrade performance; use with caution in high-throughput apps.
fix
Consider alternatives like cls-hooked or AsyncLocalStorage (built-in in Node 12+).
affects: >=1.0.0
gotchaset() with linkedTop=true ties value to the top of the call chain; may cause unexpected sharing.
fix
Use linkedTop only when you explicitly want to share across sub-chains.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: als is not defined
Missing import or require of async-local-storage.
fix
Add const als = require('async-local-storage'); or import als from 'async-local-storage';
TypeError: als.enable is not a function
Import incorrectly (e.g. default vs named export mismatch) or package not installed.
fix
Use const als = require('async-local-storage'); als.enable();
AssertionError: als.get('id') returns undefined
enable() not called before set()/get(), or scope() changed context.
fix
Ensure als.enable() is called before any async operations and scope() is used appropriately.
Error: Cannot find module 'async-local-storage'
Package not installed.
fix
Run npm install async-local-storage
async_hooks is not available in this Node version
Node <8.0.0 or running in a browser without polyfill.
fix
Upgrade Node to >=8.0.0 or use a different library that does not rely on async_hooks.
Upgrade
Version history
2.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
async-local-storage — npm install async-local-storage · libregistry