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:
- Node.js: Executing a Piece of Code after a Delay
- 2 Ways to Set Default Time Zone in Node.js
- How to Delete a File or Directory in Node.js
- Node.js: Ways to Create a Directory If It Doesn’t Exist
- Node.js: How to Use “Import” and “Require” in the Same File
- Top best Node.js Open Source Headless CMS
You can also check out our Node.js category page or PHP category page for the latest tutorials and examples.