OS Module
Inspect the operating system: CPUs, memory, platform, and network.
What the os module is for
The os module answers questions about the machine your program is running on: what operating system, how many CPU cores, how much memory, which user. It is read-only — you inspect the system, you don't change it.
Typical uses:
- Sizing a worker pool to the number of CPU cores
- Writing temp files to the right directory on any OS
- Logging platform details in bug reports and health checks
- Branching behavior per platform (rare, but sometimes necessary)
const os = require("os");
console.log("Platform:", os.platform()); // 'linux', 'darwin', 'win32'
console.log("Architecture:", os.arch()); // 'x64', 'arm64'
console.log("OS release:", os.release());
console.log("Hostname:", os.hostname());
console.log("Uptime:", Math.round(os.uptime() / 60), "minutes");CPUs and load
os.cpus() returns an array with one entry per logical core, each describing its model, speed, and time spent in various states. Mostly you just want the count — os.cpus().length — to size thread pools or cluster workers.
os.availableParallelism() (Node 18.14+) is the modern, more accurate way to ask *"how many things should I run in parallel?"* — it accounts for container CPU limits that cpus().length ignores.
const os = require("os");
const cpus = os.cpus();
console.log("Logical cores:", cpus.length);
if (cpus.length > 0) {
console.log("Model:", cpus[0].model);
}
// The better question for sizing worker pools:
console.log("Available parallelism:", os.availableParallelism());Memory
Two functions report system memory in bytes:
os.totalmem()— total physical RAMos.freemem()— currently unused RAM
Don't confuse these with process.memoryUsage(), which reports what *your process* is using. System memory numbers are useful for health endpoints and for deciding whether to take on memory-hungry work.
const os = require("os");
const gb = (n) => (n / 1024 / 1024 / 1024).toFixed(2) + " GB";
console.log("Total RAM:", gb(os.totalmem()));
console.log("Free RAM: ", gb(os.freemem()));
const used = process.memoryUsage().heapUsed;
console.log("This process heap:",
(used / 1024 / 1024).toFixed(1) + " MB");Users, directories, and line endings
A few more everyday helpers:
os.homedir()— the current user's home directory (for config files like~/.myapprc)os.tmpdir()— the right place for temporary files on this OSos.userInfo()— username, uid, home directory, and shellos.EOL— the platform's line ending:"\n"on Unix,"\r\n"on Windows
Use os.EOL when writing text files that native Windows tools (like Notepad) will open; for everything else, plain \n is standard.
const os = require("os");
console.log("Home:", os.homedir());
console.log("Temp:", os.tmpdir());
const user = os.userInfo();
console.log("Username:", user.username);
console.log("EOL bytes:", JSON.stringify(os.EOL));
// "\n" on Linux/macOS, "\r\n" on WindowsA practical example: a health snapshot
Combining the pieces yields the kind of diagnostic snapshot real services expose on a /health endpoint or write to logs at startup. Everything here is cheap to compute — safe to call on every request.
const os = require("os");
function healthSnapshot() {
return {
platform: os.platform() + " " + os.arch(),
node: process.version,
cores: os.availableParallelism(),
memory: {
freeMB: Math.round(os.freemem() / 1024 / 1024),
totalMB: Math.round(os.totalmem() / 1024 / 1024),
},
uptimeMin: Math.round(os.uptime() / 60),
};
}
console.log(JSON.stringify(healthSnapshot(), null, 2));Check your understanding
5 questionsQuestion 1 of 5
Score: 0