Nuclear Pumpkin Mocha


    1.0.7 • Public • Published

    Continuation Local Storage

    npm package Build Status Coverage Status Dependencies Status
    The purpose with this module is to share contexts across async (and sync) calls. Contexts are accessed by keys and can be nested. It is an alternative to the deprecated domain. It is based on async_hooks that were introduced in node 8. Beware that that the async_hooks are still experimental in nodejs.

    To avoid weird behavior with express

    1. Make sure you require node-cls in the first row of your app. Some popular packages use async which breaks CLS.
    2. If you are using body-parser and context is getting lost, register it in express before you register node-cls's middleware.

    Request handler
    A typical scenario is when you need to share context in a request handler.

    let http = require('http');
    let cls = require('node-cls');
    let server = http.createServer(function (request, response) {
        let context = cls.create('request-context'); = 1;
        context.request = request;
        context.response = response;  ;
    function doWork() {
        let context = cls.get('request-context');
        context.response.end(`End: ${}`) //End: 1

    Async calls
    Context is retained in async calls.

    let cls = require('node-cls');
    let context = cls.create('myContext'); => { = 'George';
        setTimeout(onTimeout, 300);
    function onTimeout() {
        let context = cls.get('myContext');
        console.log(; //George

    Contexts can be nested, even on the same key.

    let cls = require('node-cls');
    let context = cls.create('myContext'); () => { = 'George';
        let context2 = cls.create('myContext');
        console.log( //George
    async function onNested() {
        await Promise.resolve();
        let context = cls.get('myContext');
        console.log(; //undefined = 'John Nested';
        setTimeout(onTimeout, 300);
    function onTimeout() {
        let context = cls.get('myContext');
        console.log(; //John Nested

    Symbol as key
    If you are a library author, use a Symbol as key to avoid conflicts with other libraries.

    let cls = require('node-cls');
    let key = Symbol();
    let context = cls.create(key); => { = 'George';
        setTimeout(onTimeout, 300);
    function onTimeout() {
        let context = cls.get(key);
        console.log(; //George

    Await instead of run
    In node 12 and above you can start a context directly instead of wrapping it in the run function. The start function returns a promise. You can leave the current context by calling exit.

    let cls = require('node-cls');
    async function main() {
        let context = cls.create('myContext'); = 'George';
        await context.start();
        let context2 = cls.create('myContext'); = 'John Nested';
        await context2.start();
        console.log(cls.get('myContext').name); //John Nested
        console.log(cls.get('myContext').name); //George
        console.log(cls.get('myContext')); //undefined


    npm i node-cls

    DownloadsWeekly Downloads






    Unpacked Size

    16.3 kB

    Total Files


    Last publish


    • lroal