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 |
|---|---|
|
|
|
|
| Array of per-core info (model, speed, times) |
| Total / available RAM in bytes |
| The current user's home directory |
| The OS temp directory |
| The machine name |
| System uptime in seconds |
| Current user: name, uid, gid, shell, homedir |
| Network adapters and their addresses |
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:
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()
}Memory: report in human units
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
Platform-aware behavior
os.platform() lets you branch on OS — but reach for purpose-built constants where they exist, rather than re-deriving them:
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
A quick diagnostics banner
Logging the environment at startup makes bug reports far easier to triage:
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,
})