Node Js Get Total Number of System CPU Cores Tutorial

Node Js Get Total Number of System CPU Cores Tutorial

In this tutorial, you will learn how to get the number of CPU cores in Node.js.

How to Get the Number of System CPU Cores using Node js

Here are steps:

Step 1: Set Up a Node.js Project

First of all, open your terminal or cmd and run the following command into it to SET UP new node js project into your system:

mkdir get-cpu-cores
cd get-cpu-cores
npm init -y

Step 2: Create Server.js File

Next, open your node js project in any text editor. And create a new JavaScript file, for example, server.js.

Step 3: Import the os module

When you create server.js or index.js file into your node js project, then import os module into it to get CPU cores information:

// index.js

const os = require('os');

function getCPUCores() {
const numCPUs = os.cpus().length;
console.log(`Number of CPU Cores: ${numCPUs}`);


Step 4: Run Your Node.js Script

Now, you can run your script to get the number of CPU cores:

node server.js

Step 5: Get the Result in the Console Screen

You will see the following output in your browser console screen:

Number of CPU Cores: 4


That’s it; you have learned how to get the number of CPU cores in Node.js.

Recommended Node js Tutorials


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *