Node Js Get Total Number of System CPU Cores Tutorial - TechvBlogs

Node Js Get Total Number of System CPU Cores Tutorial

In this small yet profound guide, you will find out how to check the number of each CPU core available in your system with Node js.

Suresh Ramani - Author - TechvBlogs
Suresh Ramani

1 year ago

TechvBlogs - Google News

In this short tutorial, we will share the quick and straightforward way to get the total number of computer processors through the Node js application using the os module.

To obtain the operating system’s information, we will use the cpus() method, the cpus() method is available through the OS module in node js.

The os.cpus() method is a default module of node js; when invoked, it returns an array of objects holding the information regarding every CPU/Core installed on your system.

This quick tutorial will show how to call the cpus() method and get the model, speed (in MHz), and times that each logical CPU core takes.

The data returns an array with the number of processors incorporated in the system.

Here are the steps we are going to take to accomplish this node js guide.

How to Get System CPU Cores or Processors Available in Node Js

  • Step 1: Create App Directory
  • Step 2: Build Package JSON
  • Step 3: Make App.js File
  • Step 4: Get CPU Cores Numbers
  • Step 5: Display Output in Node

Create App Directory

We are about to create a new folder on your system; we will keep the files and folders related to this small project in this directory.

You may generate a folder using a single command mentioned below:

mkdir node-cpus

let us navigate to the project folder’s root:

cd node-cpus

Build Package JSON

In this step, we will show you how to configure npm in your node project. You must be thinking, why do we need to do this.

We need to run the npm initializer command because we might be needing some external packages or built-in modules to access the os system-related information.

So, after creating a brand new project directory. You must initialize the given npm command.

npm init
  "name": "node-cpus",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC"

Make App.js File

In this step, you require to create a new file at the root of your project folder, we are naming it app.js.

However, you can name it anything you want, but the app or server sounds more generic.

To know the CPU cores in the node we will be using the command-line interface.

In order to execute the command to see the output, first, you must register the command in the script section in the package.json file.

"scripts": {
   "start": "node app.js"

Get CPU Cores Numbers

In this step, you need to go to the app.js file, open the file and insert all the given code into the file.

const os = require('os');

const systemCpuCores = os.cpus();


Display Output in Node

Head over to command prompt, type the suggested command on the console’s screen then hit enter.

node app.js

After running the command, you will see the model of the total number of processors in your system.

    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2700,
    times: {
      user: 4863830,
      nice: 17060,
      sys: 1096340,
      idle: 17975530,
      irq: 0
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2683,
    times: {
      user: 4639360,
      nice: 15750,
      sys: 1175380,
      idle: 18029760,
      irq: 0
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2649,
    times: {
      user: 4802690,
      nice: 13770,
      sys: 1096340,
      idle: 17958540,
      irq: 0
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 1817,
    times: {
      user: 5105390,
      nice: 15660,
      sys: 1143620,
      idle: 17753240,
      irq: 0

Each CPU object in the array has these values:

  • model: string of the CPU model
  • speed: number of the CPU speed in MHz
  • user: number of milliseconds the CPU has spent in user mode
  • nice: number of milliseconds the CPU has spent in nice mode
  • sys: number of milliseconds the CPU has spent in sys mode
  • idle: number of milliseconds the CPU has spent in idle mode
  • irq: number of milliseconds the CPU has spent in irq mode

Given this array of objects, we can get the number of CPUs by getting the length of the array.

Here's what the code would look like:

const numOfCpus = os.cpus().length

When you log the numOfCpus, it will print a number for the amount of CPUs the system your Node.js code is running in has.

Thank you for reading this blog.

Read Also: How to Get Previous and Next Record in Laravel

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)


Note: All Input Fields are required.