Registry / devops / cls-bluebird

cls-bluebird

JSON →
library2.1.0jsnpmunverified

Patches Bluebird promises to work with continuation-local-storage (CLS), enabling automatic propagation of CLS context across promise chains. Version 2.1.0 is a complete rewrite aiming for full reliability with over 100,000 tests. Compatible with Bluebird v2.x and v3.x. The patching follows the convention of binding .then() callbacks to the context in which .then() is called. Supports coroutines, Promise.map, Promise.reduce, and other Bluebird methods. Maintained but limited to legacy CLS module (not async_hooks). For async_hooks-based CLS (e.g., cls-hooked), use alternative patches.

npm install cls-bluebird
INSTALL
IMPORT
SIG · CLS-BLUEBIRD
C
cls-bluebird
devopsjavascriptv2.1.0
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.

clsBluebird
const clsBluebird = require('cls-bluebird');
import clsBluebird from 'cls-bluebird';
Package is CommonJS-only; no ESM or default export provided. Use require().
Function call
clsBluebird(ns);
clsBluebird(ns, Promise);
Passing only the namespace patches the global bluebird instance. To patch a specific copy, pass both ns and a Promise instance.
New library copy
const Promise = require('bluebird').getNewLibraryCopy(); clsBluebird(ns, Promise);
clsBluebird(ns, require('bluebird'));
Require('bluebird') returns the same constructor each time. Use getNewLibraryCopy() for an independent copy.

Demonstrates creating a CLS namespace, patching a Bluebird copy, and verifying context propagation across promise .then() callbacks.

const cls = require('continuation-local-storage'); const ns = cls.createNamespace('myNamespace'); const bluebird = require('bluebird'); const Promise = bluebird.getNewLibraryCopy(); const clsBluebird = require('cls-bluebird'); clsBluebird(ns, Promise); ns.run(() => { ns.set('foo', 'bar'); Promise.resolve().then(() => { console.log(ns.get('foo')); // 'bar' }); }); ns.run(() => { ns.set('foo', 'baz'); setTimeout(() => { ns.run(() => { Promise.resolve().then(() => { console.log(ns.get('foo')); // 'baz' }); }); }, 10); });
Debug
Known issues
deprecatedcontinuation-local-storage package is deprecated in favor of cls-hooked or async_hooks.
fix
Consider using cls-hooked with a different promise patch (e.g., cls-bluebird-hooked).
affects: >=2.0.0
gotchaCalling clsBluebird(ns) without a second argument patches the global bluebird constructor, affecting all require('bluebird') calls.
fix
Always use getNewLibraryCopy() and pass the instance to avoid global side effects.
affects: >=2.0.0
breakingVersion 2.0.0 completely rewrote the library; old v1 API (clsBluebird(Promise)) is removed.
fix
Use clsBluebird(ns) or clsBluebird(ns, Promise) as the new API.
affects: >=2.0.0
gotchaThe patch does NOT propagate context across .catch(), .finally(), or .return() in all cases when using bluebird v2?
fix
Upgrade to 2.1.0 which improved coverage. If still broken, test with bluebird v3.
affects: >=2.0.0 <2.1.0
gotchabluebird v3's Promise.method() and Promise.try() may not propagate context correctly if not patched explicitly.
fix
Ensure you are using a supported bluebird version (2.x or 3.x) and test extensively.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: clsBluebird is not a function
CommonJS require returns an object? No, it returns a function. Likely wrong import syntax (ESM import) or wrong package name.
fix
Use const clsBluebird = require('cls-bluebird'); (CommonJS). Do NOT use import clsBluebird from 'cls-bluebird'.
Error: No namespace provided
Called clsBluebird() without arguments or with undefined.
fix
Provide a CLS namespace: clsBluebird(ns [, Promise]);
TypeError: ns.get is not a function
Passed an object that is not a CLS namespace (e.g., a string).
fix
Create a namespace via cls.createNamespace('name') and pass it.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
bluebirdrequiredPeer dependency; cls-bluebird patches bluebird promises. Must be installed separately.
continuation-local-storagerequiredPeer dependency; provides the CLS namespace. Must be installed separately.
Agent activity
6 hits · last 30 days
node
6
Resources
cls-bluebird — npm install cls-bluebird · libregistry