API reference

os

14/14Coverage13/14Strict (zero caveats)

Node's own lib/os.js, in TypeScript, over the host reads libuv makes: os.cpus() returns live per-core tick counters, os.networkInterfaces() walks the real adapters, os.release()/version() answer what Node answers on the same machine, and a failed call throws Node's ERR_SYSTEM_ERROR. os.cpus() speed reports the fixed 2400 MHz libuv nominal on Apple Silicon (where hw.cpufrequency is unavailable), cpufreq's scaling_max_freq or 0 on Linux (VMs and containers have no cpufreq), and Darwin times.irq is 0 — all exactly what real Node reports on that hardware. As in Node, the functions convert to their result: `${os.hostname}` is the host name.

Exact Works, with caveats Not supported

os.platform()

Exact
platform(): string

Returns the operating-system platform string — process.platform, as Node's platform() returns.

Returns

string — The platform identifier (e.g. "darwin", "linux").

console.log(os.platform().length > 0); // true
See it in a full example →

os.homedir()

Exact
homedir(): string

Returns the current user's home directory, mirroring libuv's uv_os_homedir: a set HOME wins (an empty one included, matching Node's ""); on POSIX an unset HOME falls back to the passwd database, and only a failed lookup throws ERR_SYSTEM_ERROR. Windows reads USERPROFILE, else the process token's profile directory.

Returns

string — The home directory path.

const home = os.homedir();
console.log(home.length > 0); // true
See it in a full example →

os.tmpdir()

Exact
tmpdir(): string

Returns the default temporary-file directory: on POSIX the first set, non-empty one of TMPDIR, TMP and TEMP (Node's getTempDir) with one trailing slash stripped, else "/tmp"; on Windows TEMP, TMP, then <SystemRoot|windir>\temp. Never throws.

Returns

string — The temp directory path.

const tmp = os.tmpdir();
console.log(tmp.length > 0); // true
See it in a full example →

os.hostname()

Exact
hostname(): string

Returns the host name of the operating system (gethostname); a failure throws ERR_SYSTEM_ERROR.

Returns

string — The host name.

const host = os.hostname();
console.log(host.length > 0); // true
See it in a full example →

os.cpus()

Exact
cpus(): { model: string; speed: number; times: { user: number; nice: number; sys: number; idle: number; irq: number } }[]

Returns real per-core {model, speed, times: {user, nice, sys, idle, irq}} objects — Linux via /proc/cpuinfo and /proc/stat parsing, Darwin via sysctlbyname and Mach host_processor_info — both verified (on Apple Silicon M4 Pro, model = "Apple M4 Pro" with live tick counters). On Apple Silicon, where hw.cpufrequency is unavailable, speed reports the fixed 2400 MHz nominal that real Node/libuv reports there; on Linux it reflects /proc/cpuinfo's current/scaled cpu MHz, matching real Node. Darwin times.irq is 0, exactly as real Node reports on Darwin.

Returns

object[] — One entry per logical core.

const cpus = os.cpus();
console.log(cpus.length > 0); // true
console.log(cpus[0].model.length > 0); // true
See it in a full example →

os.totalmem() / os.freemem()

Exact
totalmem(): number; freemem(): number

Return the total and free system memory in bytes. Verified on Linux and on Apple Silicon M4 Pro — totalmem() matches sysctl hw.memsize, and Darwin freemem() (Mach host_statistics) returns a live free-page figure.

Returns

number — Bytes of total or free memory.

const total = os.totalmem();
const free = os.freemem();
console.log(free > 0 && free <= total); // true
See it in a full example →

os.EOL

Exact
EOL: string

The operating-system-specific end-of-line marker: "\r\n" on Windows, "\n" on POSIX.

Returns

string — "\r\n" on Windows, "\n" elsewhere.

os.type() / os.release() / os.version() / os.machine()

Exact
type(): string; release(): string; version(): string; machine(): string

The uname-shaped identity strings. POSIX reads uname(); Windows reports "Windows_NT", RtlGetVersion's major.minor.build, the registry ProductName with libuv's "Windows 10" → "Windows 11" rewrite for build ≥ 22000, and x86_64/arm64 — verified byte-identical to Node on Windows 11.

Returns

string — e.g. "Linux" / "6.8.0-45-generic" / "#45-Ubuntu SMP …" / "x86_64".

console.log(os.type(), os.release(), os.machine());

os.arch() / os.endianness() / os.devNull

Exact
arch(): string; endianness(): 'BE' | 'LE'; devNull: string

process.arch; the host's byte order ("LE" or "BE"); and the null device ("\\\\.\\nul" on Windows, "/dev/null" elsewhere).

console.log(os.arch(), os.endianness(), os.devNull);

os.uptime() / os.loadavg() / os.availableParallelism()

Exact
uptime(): number; loadavg(): number[]; availableParallelism(): number

libuv's sources: seconds since boot as a double (CLOCK_BOOTTIME on Linux, kern.boottime on Darwin, GetTickCount64 on Windows); the 1/5/15-minute load averages via getloadavg, [0, 0, 0] on Windows as in Node; the CPUs this process may run on (the affinity set on Linux, the online count elsewhere, GetActiveProcessorCount on Windows), at least 1.

console.log(os.uptime() > 0, os.loadavg().length, os.availableParallelism() >= 1);

os.userInfo()

Exact
userInfo(options?: { encoding?: BufferEncoding | 'buffer' }): { uid: number; gid: number; username: string; homedir: string; shell: string | null }

The effective user's passwd entry on POSIX (uv_os_get_passwd); on Windows uid and gid are -1, username comes from GetUserNameW, homedir is os.homedir() and shell is null — exactly what Node reports. { encoding: 'buffer' } gives Buffers; a failed lookup throws ERR_SYSTEM_ERROR.

Returns

object — { uid, gid, username, homedir, shell }.

const u = os.userInfo();
console.log(u.username, u.homedir === os.homedir());

os.networkInterfaces()

Exact
networkInterfaces(): any

An object keyed by interface name, each an array of { address, netmask, family, mac, internal, cidr, scopeid } records in libuv's order, with scopeid on IPv6 entries only. getifaddrs (up + running interfaces, MAC from the link-layer entry) on POSIX, GetAdaptersAddresses on Windows; cidr is Node's own getCIDR over the netmask.

const nis = os.networkInterfaces();
for (const name of Object.keys(nis)) {
  for (const ni of nis[name]) console.log(name, ni.family, ni.address, ni.cidr);
}

os.getPriority() / os.setPriority()

Works, with caveats
getPriority(pid?: number): number setPriority(pid: number, priority: number): void setPriority(priority: number): void

A process's scheduling priority (-20 highest to 19 lowest; pid 0 is this process) through getpriority/setpriority — on Windows the priority class, mapped as libuv maps it. Node's argument validation; a failure throws ERR_SYSTEM_ERROR with the failed call's info ({ errno, code, message, syscall }).

os.setPriority(10);
console.log(os.getPriority()); // 10
try { os.getPriority(999999) } catch (e: any) { console.log(e.info.code) } // ESRCH
⚠ Differences from standard
  • The error's errno/syscall are copies of info's, not accessors over it.

os.constants

Exact
constants: { UV_UDP_REUSEADDR: number; dlopen: {...}; errno: {...}; signals: {...}; priority: {...} }

The host's dlopen flags, errno codes, signal numbers and priority levels as null-prototype objects, signals frozen, in Node's own order (node_constants.cc); on Windows errno also carries the WSA* codes.

Returns

object — { UV_UDP_REUSEADDR, dlopen, errno, signals, priority }.

console.log(os.constants.signals.SIGTERM, os.constants.errno.ENOENT, os.constants.priority.PRIORITY_HIGHEST);