Loading

Quipoin Menu

Learn • Practice • Grow

node-js / OS Module
tutorial

OS Module

Imagine you're a doctor checking a patient's vital signs – temperature, heart rate, blood pressure. In Node.js, the **OS module** lets you check the vital signs of your computer!

What is the OS Module?

The OS module provides operating system-related utility methods and properties. It gives you information about your computer's hardware, operating system, and current resource usage.

Think of the OS module as your computer's self-awareness – it can tell you what kind of computer it is, how much memory it has, how many CPU cores, and more.

Importing the OS Module
const os = require('os');

Basic System Information

console.log('Platform:', os.platform()); <!-- 'win32', 'darwin', 'linux' -->
console.log('Architecture:', os.arch()); <!-- 'x64', 'arm', 'ia32' -->
console.log('OS Type:', os.type()); <!-- 'Windows_NT', 'Linux', 'Darwin' -->
console.log('OS Release:', os.release()); <!-- '10.0.19045', '5.10.0' -->
console.log('Hostname:', os.hostname()); <!-- Your computer's name -->

CPU Information

The OS module can tell you about your CPU(s).
<!-- Number of CPU cores -->
console.log('CPU Cores:', os.cpus().length);

<!-- Details about each CPU core -->
console.log('First CPU:', os.cpus()[0]);
<!-- { model: 'Intel(R) Core(TM) i7-8700K', speed: 3700, times: {...} } -->

<!-- CPU load average (1, 5, 15 minutes) – not available on Windows -->
console.log('Load Average:', os.loadavg());

Memory Information

See how much RAM your system has and how much is free.
const totalMemory = os.totalmem(); <!-- in bytes -->
const freeMemory = os.freemem(); <!-- in bytes -->

console.log('Total Memory:', (totalMemory / 1024 / 1024 / 1024).toFixed(2), 'GB');
console.log('Free Memory:', (freeMemory / 1024 / 1024 / 1024).toFixed(2), 'GB');
console.log('Memory Usage:', ((totalMemory - freeMemory) / totalMemory * 100).toFixed(1), '%');

Network Information

Get information about your network interfaces.
const networks = os.networkInterfaces();
console.log(networks);

<!-- Get your IP address (simplified) -->
const getIPAddress = () => {
  const interfaces = os.networkInterfaces();
  for (const name of Object.keys(interfaces)) {
    for (const net of interfaces[name]) {
      if (net.family === 'IPv4' && !net.internal) {
        return net.address;
      }
    }
  }
  return '127.0.0.1';
};
console.log('Your IP:', getIPAddress());

User Information

Find out who's using the computer.
console.log('Home Directory:', os.homedir());
console.log('Temporary Directory:', os.tmpdir());
console.log('User Info:', os.userInfo());

Uptime – How Long Has Your Computer Been Running?
const uptimeSeconds = os.uptime();
const uptimeHours = Math.floor(uptimeSeconds / 3600);
const uptimeMinutes = Math.floor((uptimeSeconds % 3600) / 60);
console.log(`System uptime: ${uptimeHours} hours, ${uptimeMinutes} minutes`);

Constants in OS Module

The OS module also provides useful constants:
console.log('EOL (End of Line):', JSON.stringify(os.EOL)); <!-- 'n' on Mac/Linux, 'rn' on Windows -->
console.log('Priority constants:', os.constants.priority);

Practical Example: System Monitor
const os = require('os');

function showSystemInfo() {
  console.log('========== SYSTEM INFO ==========');
  console.log(`OS: ${os.type()} ${os.release()}`);
  console.log(`Platform: ${os.platform()} (${os.arch()})`);
  console.log(`Hostname: ${os.hostname()}`);
  console.log(`CPU Cores: ${os.cpus().length}`);
  console.log(`CPU Model: ${os.cpus()[0].model}`);
 
  const totalMem = os.totalmem() / (1024 * 1024 * 1024);
  const freeMem = os.freemem() / (1024 * 1024 * 1024);
  console.log(`Memory: ${totalMem.toFixed(2)} GB total, ${freeMem.toFixed(2)} GB free`);
  console.log(`Memory Usage: ${((1 - freeMem/totalMem) * 100).toFixed(1)}%`);
 
  const uptime = os.uptime();
  const days = Math.floor(uptime / 86400);
  const hours = Math.floor((uptime % 86400) / 3600);
  const minutes = Math.floor((uptime % 3600) / 60);
  console.log(`Uptime: ${days} days, ${hours} hours, ${minutes} minutes`);
  console.log('================================');
}

showSystemInfo();

Two Minute Drill

  • OS module gives you system information – platform, architecture, hostname.
  • `os.cpus()` returns array of CPU cores with details.
  • `os.totalmem()` and `os.freemem()` give memory info in bytes.
  • `os.networkInterfaces()` shows network configuration.
  • `os.uptime()` tells you how long the system has been running.
  • Useful for monitoring apps, load balancers, and system utilities.

Need more clarification?

Drop us an email at career@quipoinfotech.com