Sometimes your Node.js code needs to know about the machine it's running on, how much memory is free, how many CPU cores are available, or which platform it's on. The os module provides exactly that.
What Is the os Module?
os is a built-in module that exposes operating-system-level information: platform ('darwin', 'linux', 'win32'), CPU core details, total and free memory, network interfaces, and the current user's home directory, without needing any external package.
It's especially useful for health-check endpoints, deciding how many worker processes to spawn (matching CPU core count), and diagnostic logging that helps explain why an app behaves differently across environments.
os.totalmem()/os.freemem() return bytes, dividing by 1024 ** 3 converts them into a human-friendly gigabyte figure.
Common os Methods
os.platform() // 'darwin', 'linux', 'win32'
os.cpus() // array of CPU core details
os.totalmem() // total system memory in bytes
os.freemem() // free system memory in bytes
os.homedir() // current user's home directory
os.platform() identifies the operating system, useful for platform-specific logic.
os.cpus() returns an array, one entry per logical CPU core, use .length for a simple core count.
Memory methods (totalmem, freemem) return raw byte counts.
os.homedir() and os.tmpdir() give safe, cross-platform locations for user files and temp files.
os Module Cheatsheet
System information you can read directly from Node.js.
Method
Returns
os.platform()
Operating system identifier
os.arch()
CPU architecture, e.g. 'x64', 'arm64'
os.cpus()
Array of per-core CPU details
os.totalmem()
Total system memory in bytes
os.freemem()
Free system memory in bytes
os.homedir()
Current user's home directory path
os.tmpdir()
Default directory for temporary files
Scaling Workers to CPU Count
One of the most practical uses of os.cpus() is deciding how many worker processes to spawn, whether via the cluster module or worker_threads. Since Node.js's main JavaScript thread is single-threaded, matching the worker count to the number of available CPU cores makes full use of the hardware.
import os from 'node:os';
import cluster from 'node:cluster';
const cpuCount = os.cpus().length;
if (cluster.isPrimary) {
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
}
This pattern is explored fully in the Node.js Clustering lesson later in this course.
Using os for Health Checks
Basic health-check or /status endpoints often report memory and CPU information so monitoring tools can flag machines running low on resources before they fail outright.
Assuming os.freemem() reflects memory available specifically to your Node.js process rather than the whole machine.
Hardcoding worker counts instead of deriving them from os.cpus().length.
Forgetting os.loadavg() returns [0, 0, 0] on Windows, since load averages are a Unix concept.
Confusing os.platform() (the OS) with os.arch() (the CPU architecture).
Key Takeaways
The os module reads operating-system-level details like platform, CPU, and memory.
os.cpus().length is commonly used to size worker pools for clustering.
Memory-related methods return raw bytes, convert as needed for display.
os.homedir()/os.tmpdir() provide safe, cross-platform file locations.
Pro Tip
Combine os.freemem() and process.memoryUsage() in your monitoring, the former shows machine-wide pressure while the latter shows exactly how much memory your specific Node.js process is consuming.
You now know how to read system information with Node.js. Next, learn the http module, the foundation every Node.js web server is built on.