having a global object with cluster module of node.js

I am using clustering in the Rest api app built on express in node.js.

the full code for Rest API utilizing clustering is.

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var counter = {"color":{},"weight":{}};

if (cluster.isMaster) {

        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }

        cluster.on('exit', function(worker, code, signal) {         
            var exitCode = worker.process.exitCode;
            console.log('worker ' + worker.process.pid + ' died ('+exitCode+'). restarting...');
            cluster.fork();     
        });



} else {    

        var express = require("express"),
        var msg;
        var server = express();     
        server.use(express.bodyParser());
        server.post('/grouper', function (req, res) {

            //some update on global counter         

        });

        server.listen(8080);

}

My question is is there any way i could have a global object just like the counter object shown in code between the different forked instances so that all the instances update the same object?

All worker processes are indeed new copies of your application variables independently.

Each worker is a full featured process created with child_process.fork. In documents you can get following statement:

No shared state between the workers. Because workers are all separate processes, they can be killed or re-spawned depending on your program's needs, without affecting other workers

So answer is no you cannot share a variable counter, Your cluster processes don't share variables among themselves.

I did this via moving the shared resource update part to the Master like

if (cluster.isMaster) {

        for (var i = 0; i < numCPUs; i++) {

            var worker = cluster.fork();

            worker.on('message', function(msg) {

                //update counter according to attributes in msg         
                console.log(counter);

            });


        }

        // some code



} else {    

        var express = require("express"),
        var msg;
        var server = express();     
        server.use(express.bodyParser());
        server.post('/grouper', function (req, res) {

            //some update on global counter  
            msg = req.body;
            //communicate to master here
            process.send(msg);       

        });

        server.listen(8080);

}

process.send to communicate from child processes to master and keep updation part at master.