Kinda Code
Home/Node/Get the Number of CPU Cores in Node.js

Get the Number of CPU Cores in Node.js

Last updated: July 12, 2022

The standard module os of Node.js provides a lot of operating system-related utilities. The os.cpus() method returns an array of objects containing information about each logical CPU core. To count the number of CPU cores, we can use:

os.cpus().length

Example:

import os from 'os'

// For CommonJS project, use:
// const os = require('os');

const coreNumber = os.cpus().length;

// Printing the number of CPU cores
console.log(coreNumber);        

My output (yours might be different from mine):

8

Further reading:

You can also check out our Node.js category page or PHP category page for the latest tutorials and examples.