Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

os

The node:os module provides synchronous utilities and constants for inspecting the operating system, CPU, memory, directories, network interfaces, and runtime environment. It is available through ES module and CommonJS imports.

Since
v18.14.0

How it works

Import the built-in module with node:os, then call its synchronous methods to inspect the host running the Node.js process. The module exposes platform identity, CPU and memory information, directories, network interfaces, load averages, constants, and other operating-system utilities.

Syntax

import os from 'node:os';
const os = require('node:os');

Returns

The module exposes operating-system utility methods and properties; individual methods return strings, numbers, arrays, objects, or constants as documented.

Key points

  • The built-in node:os module supplies synchronous utilities for reading operating-system-related information.
  • availableParallelism() gives an estimate intended to guide the default concurrency level of a program.
  • cpus() reports information for each logical CPU core, including model, speed, and timing data.
  • hostname() returns the operating system host name as a string.
  • loadavg() returns three load-average values representing one, five, and fifteen minutes.
  • freemem() reports currently free system memory in bytes.
  • platform() identifies the operating-system platform targeted by the compiled Node.js binary.

Common mistakes

  • platform(), arch(), and endianness() describe the platform, CPU architecture, and byte order for which the Node.js binary was compiled, not necessarily every property of the host environment.
  • availableParallelism() returns a positive estimate of the parallelism a program should use; it is not a precise count of all hardware CPUs.

Example: Inspect host operating-system information

import os from 'node:os';

console.log({
  platform: os.platform(),
  architecture: os.arch(),
  hostname: os.hostname(),
  availableParallelism: os.availableParallelism(),
  totalMemory: os.totalmem(),
  freeMemory: os.freemem()
});

Prints common host properties and memory values using the node:os module.

When to use it

Choose concurrency defaults

A service needs to select a reasonable default worker or connection-pool size based on the host's available parallelism.

Expose runtime diagnostics

A diagnostic endpoint or startup log needs host identity, platform, architecture, and memory information.

Resolve operating-system directories

A command-line tool must locate the current user's home or the system temporary directory without hard-coding paths.

Collect host metrics

A monitoring component needs CPU, memory, load-average, or network-interface information from the host.

Interview questions

QHow do you import and use the os module?

The module is imported as node:os, using either ES modules or CommonJS. Its methods synchronously return operating-system information such as the hostname, platform, memory totals, and CPU details.

Follow-up: How would you choose worker concurrency? Use availableParallelism() as the documented estimate of the default parallelism a program should use, while still applying application-specific limits.

QWhat is the difference between totalmem() and freemem()?

totalmem() and freemem() return memory quantities in bytes. They describe the system's total and currently free memory, so callers should treat the values as runtime host information rather than process-specific heap measurements.

Follow-up: How would you inspect process heap usage instead? Use Node.js process or v8 APIs rather than interpreting os.freemem() as the JavaScript heap's available space.

QIs loadavg() portable across operating systems?

What loadavg() returns depends on the operating system: it provides the 1-, 5-, and 15-minute load averages where supported. On Windows, it returns an empty array.

Follow-up: What portable alternative can be used when load averages are unavailable? Combine availableParallelism(), CPU utilization measurements, and application-level latency or queue metrics.

  • availableParallelism()
  • arch()
  • cpus()
  • endianness()
  • freemem()
  • homedir()
  • hostname()
  • loadavg()

Source: OS | Node.js v26.12.0 Documentation. Checked 2026-10-15.