Node.js OS Module
The os module is a built-in Node.js module that provides information about the operating system on which the Node.js application is running. It allows a program to access details such as the computer's platform, available memory, CPU details, home directory, and more — all without any additional installation.
This module is particularly useful when building tools that behave differently based on the operating system, or when monitoring system resources within an application.
Loading the OS Module
const os = require('os');
Getting Basic System Information
os.platform() – Operating System Platform
Returns a string identifying the operating system platform:
const os = require('os');
console.log(os.platform());
Possible return values:
'win32'— Windows (even on 64-bit systems)'darwin'— macOS'linux'— Linux
os.type() – OS Type Name
Returns a more descriptive name of the operating system:
console.log(os.type());
// Windows_NT | Darwin | Linux
os.arch() – CPU Architecture
Returns the CPU architecture for which the Node.js binary was compiled:
console.log(os.arch());
// x64 | arm | arm64 | ia32
os.release() – OS Release Version
Returns the release version of the operating system:
console.log(os.release());
// Example: 5.15.0-91-generic (Linux kernel version)
Memory Information
os.totalmem() – Total System Memory
Returns the total amount of system memory in bytes:
const os = require('os');
const totalMemoryBytes = os.totalmem();
const totalMemoryGB = (totalMemoryBytes / (1024 ** 3)).toFixed(2);
console.log("Total Memory:", totalMemoryGB + " GB");
os.freemem() – Free System Memory
Returns the amount of free (available) system memory in bytes:
const freeMemoryBytes = os.freemem();
const freeMemoryGB = (freeMemoryBytes / (1024 ** 3)).toFixed(2);
console.log("Free Memory:", freeMemoryGB + " GB");
Practical Example – Memory Usage Summary
const os = require('os');
const totalMem = os.totalmem();
const freeMem = os.freemem();
const usedMem = totalMem - freeMem;
console.log("=== Memory Report ===");
console.log("Total Memory : " + (totalMem / 1e9).toFixed(2) + " GB");
console.log("Used Memory : " + (usedMem / 1e9).toFixed(2) + " GB");
console.log("Free Memory : " + (freeMem / 1e9).toFixed(2) + " GB");
Output (example):
=== Memory Report ===
Total Memory : 15.93 GB
Used Memory : 9.47 GB
Free Memory : 6.46 GB
CPU Information
os.cpus() – CPU Core Details
Returns an array of objects, each representing a logical CPU core with details about its model, speed, and time spent in various states:
const os = require('os');
const cpus = os.cpus();
console.log("Number of CPU cores:", cpus.length);
console.log("CPU Model:", cpus[0].model);
console.log("CPU Speed:", cpus[0].speed, "MHz");
Output (example):
Number of CPU cores: 8
CPU Model: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
CPU Speed: 3600 MHz
User and Network Information
os.homedir() – Home Directory
Returns the path of the current user's home directory:
console.log(os.homedir());
// Linux/macOS: /home/alice
// Windows: C:\Users\alice
os.hostname() – Machine Hostname
Returns the hostname of the computer:
console.log(os.hostname());
// Example: alice-macbook-pro
os.userInfo() – Current User Details
Returns information about the currently logged-in user:
const os = require('os');
const user = os.userInfo();
console.log("Username:", user.username);
console.log("Home Directory:", user.homedir);
console.log("Shell:", user.shell);
os.networkInterfaces() – Network Interface Details
Returns details about all network interfaces on the machine, including IP addresses:
const os = require('os');
const interfaces = os.networkInterfaces();
console.log(interfaces);
This is useful in server applications where the actual IP address of the machine needs to be determined programmatically.
Other Useful Properties
os.uptime() – System Uptime
Returns the number of seconds the system has been running since it was last started:
const uptimeSeconds = os.uptime();
const uptimeHours = (uptimeSeconds / 3600).toFixed(2);
console.log("System has been running for:", uptimeHours, "hours");
os.tmpdir() – Temporary Directory Path
Returns the path of the operating system's default temporary files directory:
console.log(os.tmpdir());
// Linux/macOS: /tmp
// Windows: C:\Users\alice\AppData\Local\Temp
os.EOL – End of Line Marker
os.EOL is a string constant that represents the end-of-line marker used by the current operating system:
- On Linux/macOS:
\n(newline character) - On Windows:
\r\n(carriage return + newline)
const os = require('os');
const lines = ["Line one", "Line two", "Line three"];
const output = lines.join(os.EOL);
console.log(output);
Using os.EOL instead of hardcoding \n makes code cross-platform compatible.
Practical Full Example – System Information Reporter
const os = require('os');
console.log("========== System Information ==========");
console.log("OS Platform :", os.platform());
console.log("OS Type :", os.type());
console.log("Architecture :", os.arch());
console.log("Hostname :", os.hostname());
console.log("Username :", os.userInfo().username);
console.log("Home Directory :", os.homedir());
console.log("CPU Cores :", os.cpus().length);
console.log("Total Memory :", (os.totalmem() / 1e9).toFixed(2), "GB");
console.log("Free Memory :", (os.freemem() / 1e9).toFixed(2), "GB");
console.log("System Uptime :", (os.uptime() / 3600).toFixed(2), "hours");
console.log("Temp Directory :", os.tmpdir());
Key Points
- The
osmodule is built into Node.js and requires no installation. os.platform()identifies the OS;os.arch()identifies the CPU architecture.os.totalmem()andos.freemem()return memory values in bytes.os.cpus()returns an array with information about each CPU core.os.homedir()andos.hostname()provide user and machine identity details.os.uptime()shows how long the system has been running.os.EOLgives the correct line-ending character for the current OS, enabling cross-platform text handling.
