The built-in node:os module exposes read-only operating system details such as CPU core counts, free and total RAM, hostname, network interfaces, and temporary folder paths. Backend services use node:os to scale worker pools dynamically, build cross-platform temp file paths, and report server health metrics.
Instead of hardcoding 4 worker threads or /tmp file paths (which break on Windows), query os.availableParallelism() (Node 18.14+) and os.tmpdir() at runtime.
import os from 'node:os';
const totalGb = (os.totalmem() / 1024 ** 3).toFixed(2);
const freeGb = (os.freemem() / 1024 ** 3).toFixed(2);
console.log('Platform:', os.platform(), os.arch());
console.log('CPU Parallelism:', os.availableParallelism());
console.log(`Memory: ${freeGb} GB free / ${totalGb} GB total`);
console.log('Temp Directory:', os.tmpdir());
console.log('System Uptime (hrs):', (os.uptime() / 3600).toFixed(1));
os.EOL and os.tmpdir() SafelyWindows text files use line endings while Linux and macOS use . Writing os.EOL ensures generated CLI reports or CSV exports use the host OS's native newline convention automatically, and os.tmpdir() returns the right temporary directory across every OS.
os.cpus().length inside Docker containers: Inside cgroup-limited containers, os.cpus() can report all 64 host cores even when your container is capped at 2 vCPUs. Use os.availableParallelism(), which respects container limits on modern Node.js./tmp/ paths: /tmp/ fails on Windows machines. Always combine path.join(os.tmpdir(), 'my-file.tmp').os.hostname() or os.networkInterfaces() in public API responses: Internal server hostnames and LAN IP addresses help attackers map your infrastructure. Keep detailed OS telemetry on authenticated admin health checks only.os.platform() and os.type()?os.platform() returns concise Node identifiers like 'win32', 'linux', or 'darwin', whereas os.type() returns OS kernel names like 'Windows_NT', 'Linux', or 'Darwin'.
Call os.homedir(), which works reliably on Windows, macOS, and Linux without reading process.env.HOME manually.
os.freemem() the exact available memory inside Docker?Inside some containers, os.freemem() may reflect host memory rather than the cgroup limit; use process.memoryUsage() to monitor your own Node.js process heap.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.