Lesson

Node.js OS Module

10 min reading
Free Course

Node.js OS Module: Inspecting Host Hardware and System Paths

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.

Checking CPU Cores, Memory, and Platform

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));

Using os.EOL and os.tmpdir() Safely

Windows 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.

Common Mistakes to Avoid

  • Sizing worker pools blindly with 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.
  • Hardcoding /tmp/ paths: /tmp/ fails on Windows machines. Always combine path.join(os.tmpdir(), 'my-file.tmp').
  • Exposing raw 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.

Frequently Asked Questions

What is the difference between 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'.

How do I get the current user's home directory in Node.js?

Call os.homedir(), which works reliably on Windows, macOS, and Linux without reading process.env.HOME manually.

Is 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Node.js Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum