So basically I am building an express
server and I recently looked into the topic of scaling NodeJS apps. One of the first things I saw was the built in cluster
module to take advantage of all the threads of the machine's processor. Here is my code of the implementation I've made:
import cluster from "cluster";
import { cpus } from "os";
import dotenv from "dotenv";
dotenv.config();
import express, { Express } from "express";
import log from "./logger";
import database from "./database";
import router from "./router";
const app: Express = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const port = <number>(<unknown>process.env.PORT);
const host = <string>process.env.HOST;
const numCPU = cpus().length;
if (cluster.isPrimary) {
for (let i = 0; i < numCPU; i++) {
cluster.fork();
}
cluster.on("listening", (worker) => {
// Not printing anything
console.log(worker.process.pid);
});
cluster.on("exit", (worker, code, signal) => {
log.error(`Worker ${worker.process.pid} died. Starting a new worker...`);
cluster.fork();
});
} else {
app.listen(port, host, () => {
// Only prints this once.
log.info(`Server ${process.pid} listening at http://${host}:${port}`);
database();
router(app);
});
}
The problem is that the cluster.isPrimary
block never runs meaning of course that no other processes are forked by the cluster. However when I use cluster.isMaster
, everything works exactly as intended. But since isMaster is deprecated and is replaced by isPrimary, thats what I'm trying to use but it just doesn't work.
Solutions I've tried that don't work:
- Kill all node processes on my machine.
- Remove everything from this main file leaving just the barebone app and cluster configuration.
What am I doing wrong?
So basically I am building an express
server and I recently looked into the topic of scaling NodeJS apps. One of the first things I saw was the built in cluster
module to take advantage of all the threads of the machine's processor. Here is my code of the implementation I've made:
import cluster from "cluster";
import { cpus } from "os";
import dotenv from "dotenv";
dotenv.config();
import express, { Express } from "express";
import log from "./logger";
import database from "./database";
import router from "./router";
const app: Express = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const port = <number>(<unknown>process.env.PORT);
const host = <string>process.env.HOST;
const numCPU = cpus().length;
if (cluster.isPrimary) {
for (let i = 0; i < numCPU; i++) {
cluster.fork();
}
cluster.on("listening", (worker) => {
// Not printing anything
console.log(worker.process.pid);
});
cluster.on("exit", (worker, code, signal) => {
log.error(`Worker ${worker.process.pid} died. Starting a new worker...`);
cluster.fork();
});
} else {
app.listen(port, host, () => {
// Only prints this once.
log.info(`Server ${process.pid} listening at http://${host}:${port}`);
database();
router(app);
});
}
The problem is that the cluster.isPrimary
block never runs meaning of course that no other processes are forked by the cluster. However when I use cluster.isMaster
, everything works exactly as intended. But since isMaster is deprecated and is replaced by isPrimary, thats what I'm trying to use but it just doesn't work.
Solutions I've tried that don't work:
- Kill all node processes on my machine.
- Remove everything from this main file leaving just the barebone app and cluster configuration.
What am I doing wrong?
Share Improve this question edited Aug 30, 2021 at 6:57 user16622035 asked Aug 30, 2021 at 4:26 darkstardarkstar 1,00815 silver badges41 bronze badges 2-
1
What Node version are you using? Because
isPrimary
was added in v16, deprecatingisMaster
at that moment. If you're on v14 or v12,isPrimary
doesn't exist (hence it's falsy). To ply with all versions (if you need that), you couldif (cluster.isPrimary || cluster.isMaster)
. Or more cleanly:if (!cluster.isWorker)
. – Stock Overflaw Commented Aug 30, 2021 at 12:25 - @StockOverflaw you sir are 100% correct. Thank you! – darkstar Commented Aug 30, 2021 at 18:20
2 Answers
Reset to default 4Its most likely the version of node you are running. isPrimary is only available in versions 16+ and you most likely have the LTS version which is v14
Some time ago, cluster.isMaster
added in NODE version v0.8.1 is deprecated since version 16.0.0. Simply replace deprecated cluster.isMaster
by cluster.isPrimary
(as you did) only if your NODE version is at least 16.0.0.
If you don't know your version, enter node -v
and hit enter in your terminal. If you need to update your NODE version, you have different options depending on your operating system. In case you run Windows or mac on your PC, you can - for instance - go to the official NODE website and download and install the newest release: https://nodejs/en/download/current/
Background
cluster.isPrimary
returns true if the process is a primary. In contrast, you can use cluster.isWorker
to return true if the process is not a primary. So it is the negation for cluster.isPrimary
.
Many blog posts, articles and tutorials out there are still applying the deprecated cluster.isMaster
. This leads to irritations amongst node user dealing first time with NODE clustering.
Official information are provided by NODE here: https://nodejs/api/cluster.html#clusterismaster