Node Js Get System’s CPU Cores Information Tutorial

Every wondered JavasCript can also show you system-related information. If you work in a Node environment, you can easily pull out the number of processors available on your system.

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.

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 Number and Information of CPU Processors Available in Node

  • 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-proxima

Without further ado, let us navigate to project folder’s root:

cd node-proxima

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

A new package.json file is manifested in your project, you may check it is holding some information about your project.

{
  "name": "node-proxima",
  "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 app or server sounds more generic.

To know the CPU cores in 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();

console.log(systemCpuCores);

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.

The following result will be entirely diffrent from mine.

[
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 988790, nice: 0, sys: 460430, idle: 6909840, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 60920, nice: 0, sys: 26690, idle: 8265890, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 835950, nice: 0, sys: 312520, idle: 7205050, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 67340, nice: 0, sys: 23300, idle: 8262860, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 681110, nice: 0, sys: 229760, idle: 7442640, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 69720, nice: 0, sys: 19350, idle: 8264440, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 560740, nice: 0, sys: 165710, idle: 7627070, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 69710, nice: 0, sys: 16520, idle: 8267270, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 473410, nice: 0, sys: 128490, idle: 7751620, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 62570, nice: 0, sys: 14650, idle: 8276280, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 357260, nice: 0, sys: 89030, idle: 7907220, irq: 0 }
  },
  {
    model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz',
    speed: 2600,
    times: { user: 55450, nice: 0, sys: 13220, idle: 8284830, irq: 0 }
  }
]

Furthermore, we also used the length property to get the total number of System CPU cores in the node.

12

Summary

How to Get Number of System CPU Cores in Node Js

In this quick tutorial, we have found out how to count the number of logical CPUs on the system where the node js code is running.

We took the help of the default node js os (operating system) module, and we explored step by step how to import the os module,

Access the os.cpus() method and obtain the information in an object form through the cpus() method.