Beginner LevelNode JS
How do you inspect system hardware metrics using the node:os module?
❓ Interview Prompt: Explain `node:os` module, fetching CPU cores (`os.cpus()`), memory stats (`os.freemem()`, `os.totalmem()`), hostname, and network interfaces.
The 'node:os' module exposes operating system hardware and system metrics:
•
'os.cpus()': Returns array of CPU cores with clock speed and CPU time metrics.
•
'os.freemem()' / 'os.totalmem()': Returns system RAM metrics in bytes.
•
'os.networkInterfaces()': Returns assigned IP addresses per network interface.
Code Implementation Example
javascript
import os from 'node:os';
const cpuCount = os.cpus().length;
const freeMemMB = (os.freemem() / 1024 / 1024).toFixed(2);
const totalMemMB = (os.totalmem() / 1024 / 1024).toFixed(2);
console.log('OS Platform: ${os.platform()} (${os.arch()})');
console.log('CPU Cores: ${cpuCount}');
console.log('RAM: ${freeMemMB}MB free / ${totalMemMB}MB total');💡Inspecting system CPU cores and RAM metrics using node:os.
🎯 Interview Key Takeaway
Use node:os to detect CPU core counts when configuring cluster workers or thread pools.
#os#CPU#RAM#System Metrics#Beginner
Back to all Node JS Questions→