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.
cluster.isPrimary is the modern property introduced in Node.js v16.0.0
cluster.isMaster is deprecated but still functional in older codebases
cluster.isWorker is the complement of cluster.isPrimary
process.pid gives the OS-level process ID for both master and workers