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
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
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
1. Which os method returns a value like 'linux', 'darwin', or 'win32'?
2. What does os.cpus() return?
3. In what unit does os.totalmem() report memory?
4. Which os method is commonly used to decide how many worker processes to spawn with the cluster module?
Was this page helpful?
You May Also Like
The path Module in Node.js
Understand how to build, parse, and normalize file system paths cross-platform using Node's built-in path module.
Node.js Performance Optimization
Learn how to scale Node.js apps with the cluster module, avoid blocking the event loop, and apply caching strategies.
Node.js Architecture and the Event Loop
Understand Node.js's single-threaded, non-blocking architecture and how the event loop phases process callbacks.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics