08 / 17

How do you check if the current process is a master or worker in Node.js?

You can use cluster.isPrimary (or the deprecated cluster.isMaster) to check if the current process is the master, and cluster.isWorker to check if it is a worker process.

The cluster module exposes boolean properties that let you branch your code logic based on whether the current process is the primary/master or a worker. This is the foundational pattern used in every cluster-based Node.js application.

Checking Process Type
Important Notes
  1. 1

    cluster.isPrimary is the modern property introduced in Node.js v16.0.0

  2. 2

    cluster.isMaster is deprecated but still functional in older codebases

  3. 3

    cluster.isWorker is the complement of cluster.isPrimary

  4. 4

    process.pid gives the OS-level process ID for both master and workers