System Information with os Methods
You'll learn how to query hardware and system resource details using Node.js's os module methods.
Reading Your Computer's Vitals
Just as a doctor checks your pulse, temperature, and blood pressure to understand your health, Node.js can check your computer's CPU count, memory usage, uptime, and network setup. The os module provides methods that act like diagnostic tools for your system.
Core System Query Methods
The os module gives you five powerful methods to inspect system resources:
os.cpus() returns an array of objects describing each CPU core—including model, speed, and time spent in different modes. Think of it as getting a detailed spec sheet for each processor in your machine.
os.totalmem() returns the total system memory in bytes. This tells you the full RAM capacity installed on the system.
os.freemem() returns currently available memory in bytes. Like checking how much gas is left in your tank, this shows memory that's not currently in use.
os.uptime() returns the number of seconds the system has been running since last boot. Perfect for monitoring server stability or calculating when maintenance windows might be needed.
os.networkInterfaces() returns an object containing network interface information—IP addresses, MAC addresses, and interface names. This is like getting a map of all your computer's network connections.
const os = require('os');
console.log(`CPU cores: ${os.cpus().length}`);
console.log(`Total memory: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)} GB`);
console.log(`Free memory: ${(os.freemem() / 1024 / 1024 / 1024).toFixed(2)} GB`);
console.log(`System uptime: ${(os.uptime() / 3600).toFixed(2)} hours`);
console.log('Network interfaces:', Object.keys(os.networkInterfaces()));
Key Takeaway: The os module's query methods let you programmatically inspect CPU, memory, uptime, and network details, enabling system monitoring and resource-aware applications.