100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Node.js

The os Module in Node.js

Explore how to retrieve operating system information like platform, CPU cores, and memory using Node's os module.

Core ModulesBeginner7 min readJul 8, 2026
Analogies

Introduction

The os module provides operating system-related utility methods and properties. It lets a Node.js application inspect the environment it's running on, such as the platform (Windows, Linux, macOS), CPU architecture and core count, total and free memory, network interfaces, and the current user's home directory. This is useful for writing cross-platform tools, scaling worker processes to CPU count, or displaying diagnostic system information.

🏏

Cricket analogy: The os module is like a groundskeeper's report on pitch conditions, weather, and ground dimensions before a match, letting the captain (application) tailor strategy to the specific venue rather than assuming every ground is the same.

Syntax

javascript
const os = require('os');

os.platform();
os.arch();
os.cpus();
os.totalmem();
os.freemem();
os.homedir();
os.hostname();
os.uptime();

Explanation

os.platform() returns a string like 'linux', 'darwin', or 'win32' identifying the operating system. os.arch() returns the CPU architecture such as 'x64' or 'arm64'. os.cpus() returns an array of objects describing each logical CPU core, including its model and speed, which is commonly used to decide how many worker processes to spawn (for example with the cluster module). os.totalmem() and os.freemem() return memory sizes in bytes. os.homedir() returns the current user's home directory path, and os.uptime() returns the system uptime in seconds.

🏏

Cricket analogy: os.platform() and os.arch() are like checking whether the match is Test, ODI, or T20 (linux/darwin/win32) and the pitch type (x64/arm64); os.cpus() is like counting available bowlers to decide rotation (cluster sizing); os.totalmem()/os.freemem() are like checking squad depth remaining, and os.uptime() is match time elapsed.

Example

javascript
const os = require('os');

console.log('Platform:', os.platform());
console.log('Architecture:', os.arch());
console.log('CPU cores:', os.cpus().length);
console.log('Total memory (MB):', Math.round(os.totalmem() / 1024 / 1024));
console.log('Free memory (MB):', Math.round(os.freemem() / 1024 / 1024));
console.log('Home directory:', os.homedir());
console.log('Uptime (hours):', (os.uptime() / 3600).toFixed(2));

Output

On a typical Linux server, the output might look like: 'Platform: linux', 'Architecture: x64', 'CPU cores: 4', 'Total memory (MB): 8192', 'Free memory (MB): 2130', 'Home directory: /home/user', and 'Uptime (hours): 12.45'. The exact values depend entirely on the machine running the script and will differ between development laptops, CI runners, and production servers.

🏏

Cricket analogy: A diagnostic printout for a home ground (Platform: linux, 4 cores, 8GB memory, 12.45 hours since last reboot) is like a groundskeeper's report before a Test — the exact pitch conditions differ from ground to ground, just as os module values vary between machines.

Key Takeaways

  • os.platform() and os.arch() identify the operating system and CPU architecture.
  • os.cpus() returns per-core details and is commonly used to size worker pools via the cluster module.
  • os.totalmem() and os.freemem() report memory in bytes, useful for monitoring resource usage.
  • os.homedir() and os.hostname() expose environment identity information.
  • The os module is read-only and provides system information rather than modifying system state.

Practice what you learned

Was this page helpful?

Topics covered

#NodeJs#NodeJsExpressStudyNotes#WebDevelopment#TheOsModuleInNodeJs#Module#Node#Syntax#Explanation#StudyNotes#SkillVeris