NodeJSThe os Module

The os Module

node:os reports facts about the machine and operating system your process runs on — CPU cores, total and free memory, platform, architecture, network interfaces, the user's home directory, and more. It is read-only system introspection, used for tuning concurrency, picking platform-correct paths, and logging diagnostics.

The most-used calls

Call

Returns

os.platform()

'linux', 'darwin', 'win32' — the OS

os.arch()

'x64', 'arm64' — the CPU architecture

os.cpus()

Array of per-core info (model, speed, times)

os.totalmem() / os.freemem()

Total / available RAM in bytes

os.homedir()

The current user's home directory

os.tmpdir()

The OS temp directory

os.hostname()

The machine name

os.uptime()

System uptime in seconds

os.userInfo()

Current user: name, uid, gid, shell, homedir

os.networkInterfaces()

Network adapters and their addresses

JS
import os from 'node:os'

console.log(os.platform())   // 'linux'
console.log(os.arch())       // 'x64'
console.log(os.cpus().length) // 8
console.log(os.homedir())    // '/home/ada'
Sizing concurrency to the CPU

The classic use of os.cpus().length is deciding how many worker processes or threads to spawn — matching parallelism to the hardware:

JS
import os from 'node:os'
import cluster from 'node:cluster'

const workers = os.cpus().length      // one worker per logical core
if (cluster.isPrimary) {
  for (let i = 0; i < workers; i++) cluster.fork()
}
`cpus().length` counts logical cores
The number includes hyper-threads, not just physical cores — an 8-core CPU with SMT reports 16. For modern container deployments, prefer `os.availableParallelism()` (Node 18.14+): it respects CPU limits set by the container/cgroup, so it returns the cores you can *actually* use, not the host's total.
Memory: report in human units

JS
import os from 'node:os'

const gb = (bytes) => (bytes / 1024 ** 3).toFixed(2)
console.log(`Total: ${gb(os.totalmem())} GB`)
console.log(`Free:  ${gb(os.freemem())} GB`)
Total: 16.00 GB
Free:  5.42 GB
`os.freemem()` is system-wide, not your process
`os.totalmem`/`os.freemem` describe the *whole machine*, not your Node process. To measure *your* program's footprint, use `process.memoryUsage()` (heap, RSS, external). And on a container, `os.totalmem()` typically reports the *host's* RAM, ignoring cgroup limits — so don't size your heap from it blindly.
Platform-aware behavior

os.platform() lets you branch on OS — but reach for purpose-built constants where they exist, rather than re-deriving them:

JS
import os from 'node:os'

const isWindows = os.platform() === 'win32'

// Use the dedicated helpers instead of hardcoding:
console.log(os.EOL)       // '\n' on POSIX, '\r\n' on Windows — line ending
console.log(os.tmpdir())  // correct temp dir per platform
console.log(os.homedir()) // correct home dir per platform
Prefer the helper over the string compare
When you find yourself writing `platform() === 'win32' ? '\r\n' : '\n'`, stop — `os.EOL` already is that. Likewise `os.tmpdir()` beats guessing `/tmp` vs `C:\Temp`. The helpers are correct on platforms you haven't tested on.
A quick diagnostics banner

Logging the environment at startup makes bug reports far easier to triage:

JS
import os from 'node:os'

console.log({
  platform: os.platform(),
  arch: os.arch(),
  cores: os.availableParallelism?.() ?? os.cpus().length,
  totalMemGB: (os.totalmem() / 1024 ** 3).toFixed(1),
  node: process.version,
})
Next
Tools for debugging, formatting, and bridging old callback APIs to promises: [The util Module](/nodejs/util-module).