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

Node.js · Module

node:os

The node:os module provides synchronous utilities and constants for inspecting the operating system around a Node.js process. It exposes host identity, CPU and memory information, directories, network interfaces, load, uptime, and platform conventions.

How it works

Import the built-in module with the node:os specifier, then call its synchronous functions to inspect the host platform, CPU architecture, available parallelism, memory, directories, network interfaces, and other operating-system characteristics. Most values describe the environment where the Node.js process runs, so applications should avoid assuming one platform-specific result.

Syntax

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

Returns

The module exposes operating-system utility functions and constants; individual APIs return strings, numbers, arrays, objects, or platform-specific constants as documented.

Key points

  • Use this built-in module when application logic needs basic operating-system information.
  • ES modules can import the module with the node:os specifier.
  • CommonJS programs can load the same module with require.
  • availableParallelism() supplies a recommended parallelism estimate and always returns a positive value.
  • loadavg() returns Unix-style one-, five-, and fifteen-minute load averages.
  • cpus() provides per-logical-core details such as model, speed, and CPU-time counters.
  • freemem() measures currently available system memory in bytes.
  • tmpdir() discovers the platform’s default temporary directory.

Example: Inspect host information

import os from 'node:os';

console.log({
  platform: os.platform(),
  architecture: os.arch(),
  cpus: os.availableParallelism(),
  memoryGiB: (os.totalmem() / 2 ** 30).toFixed(2),
  tempDirectory: os.tmpdir()
});

Prints portable host details using synchronous operating-system helpers.

When to use it

Estimate available parallelism

Choose a sensible default number of workers or concurrent tasks based on the runtime environment.

Build environment diagnostics

Report runtime diagnostics such as platform, architecture, hostname, memory, and uptime.

Locate standard directories

Create portable file-handling code that uses the operating system’s temporary or home directory.

Discover network interfaces

Inspect interfaces and addresses when exposing service diagnostics or selecting network behavior.

Interview questions

QWhat is the purpose of node:os?

The module is a built-in collection of operating-system utility methods and properties. It is imported with node:os, using either ESM or CommonJS syntax, and its APIs are generally synchronous.

Follow-up: How would you choose a worker count? Use availableParallelism() rather than assuming cpus().length is the correct default, because it estimates the parallelism available to the program.

QWhat do os.platform() and os.arch() return?

platform() identifies the platform for which the Node.js binary was compiled, while arch() identifies its CPU architecture. They are useful for selecting platform-specific behavior, but should not be confused with arbitrary runtime machine metadata.

Follow-up: How would you distinguish a compiled target from the host kernel? Compare these APIs with the application’s deployment assumptions; the documentation defines both values in terms of the Node.js binary.

QWhich node:os APIs are useful for resource and directory discovery?

totalmem() and freemem() return memory quantities in bytes. homedir(), tmpdir(), and hostname() return environment-dependent strings, so code should treat them as runtime values rather than fixed paths or names.

Follow-up: What should be done before placing sensitive files in tmpdir()? Apply application-level permissions and cleanup policies; the module only identifies the operating system’s temporary-file directory.

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

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