Choose concurrency defaults
A service needs to select a reasonable default worker or connection-pool size based on the host's available parallelism.
Node.js · Module
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.
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.
import os from 'node:os';
const os = require('node:os');The module exposes operating-system utility methods and properties; individual methods return strings, numbers, arrays, objects, or constants as documented.
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.
A service needs to select a reasonable default worker or connection-pool size based on the host's available parallelism.
A diagnostic endpoint or startup log needs host identity, platform, architecture, and memory information.
A command-line tool must locate the current user's home or the system temporary directory without hard-coding paths.
A monitoring component needs CPU, memory, load-average, or network-interface information from the host.
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.
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.
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()