5

Node says that cluster.isMaster is deprecated and we should use cluster.isPrimary. But, while isMaster is returning me true without problem, I getting undefined when I try cluster.isPrimary:

const cluster = require("cluster");

console.log(cluster.isPrimary); /// undefined

console.log(cluster.isMaster); /// true

Why is this happening?

bahejam379
  • 105
  • 1
  • 6

4 Answers4

4

Make sure you are on at least Node version 16.0.0

you can see this by running this command in your terminal:

node --version
about14sheep
  • 1,813
  • 1
  • 11
  • 18
  • i'm in the current LTS, version 14.18.1 – bahejam379 Oct 17 '21 at 21:15
  • as per node documentation isPrimary was added in version 16.0.0 of nodejs. You should update your node version to the latest release to use that functionality. isPrimary docs: https://nodejs.org/api/cluster.html#cluster_cluster_isprimary nodejs download: https://nodejs.org/en/ – about14sheep Oct 17 '21 at 22:01
4

Use node v16+ and add "esModuleInterop": true into tsconfig.json file.

Ion Leu
  • 41
  • 3
1

In Node 18, I imported the module as suggested in an example from the official docs:

import cluster from 'node:cluster';

I do have Typescript 4.2, and I do have the "esModuleInterop": true flag set in tsconfig.json and worked.

Mariano Ruiz
  • 4,314
  • 2
  • 38
  • 34
0

I used this statement and with node v19 and works for me:

import cluster, { Worker } from 'cluster'
Tyler2P
  • 2,324
  • 26
  • 22
  • 31