The node:os module provides operating system utility methods for retrieving information about host hardware, system uptime, available CPUs, total/free memory, network interfaces, and user profile directories.
flowchart TD
A["node:os Module"] --> B["CPU Cores & Load Metrics (os.cpus())"]
A --> C["System Memory Allocation (os.freemem(), os.totalmem())"]
A --> D["Network Interface Cards (os.networkInterfaces())"]
A --> E["Host OS Details (os.platform(), os.uptime())"]
import os from 'node:os';
function getSystemMetrics() {
const cpus = os.cpus();
const totalMemoryGB = (os.totalmem() / (1024 ** 3)).toFixed(2);
const freeMemoryGB = (os.freemem() / (1024 ** 3)).toFixed(2);
const memoryUsagePercent = (((os.totalmem() - os.freemem()) / os.totalmem()) * 100).toFixed(1);
return {
hostname: os.hostname(),
platform: os.platform(),
architecture: os.arch(),
cpuModel: cpus[0].model,
coreCount: cpus.length,
totalMemory: `${totalMemoryGB} GB`,
freeMemory: `${freeMemoryGB} GB`,
memoryUsage: `${memoryUsagePercent}%`,
uptimeHours: (os.uptime() / 3600).toFixed(2),
userHomeDir: os.homedir()
};
}
console.log('--- Host System Diagnostic Metrics ---');
console.table(getSystemMetrics());
os.cpus().length: Use CPU core counts to configure cluster process instances or worker thread pool capacities.os.freemem() in long-running node microservices to trigger alerts before out-of-memory (OOM) crashes occur.os.EOL for end-of-line line breaks ( on Linux/macOS vs `` on Windows) to keep log files portable.
Write a short function that checks if host system free memory falls below 15% of total memory and returns a boolean warning flag.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With