On this page

    P

    cluster.worker

    History
    Type:Object

    A reference to the current worker object. Not available in the primary process.

    import cluster from 'node:cluster';
    
    if (cluster.isPrimary) {
      console.log('I am primary');
      cluster.fork();
      cluster.fork();
    } else if (cluster.isWorker) {
      console.log(`I am worker #${cluster.worker.id}`);
    }
    const cluster = require('node:cluster');
    
    if (cluster.isPrimary) {
      console.log('I am primary');
      cluster.fork();
      cluster.fork();
    } else if (cluster.isWorker) {
      console.log(`I am worker #${cluster.worker.id}`);
    }