OS Module

The os module provides information about the operating system your Node.js process is running on. It is useful for scripts and tools that need to behave differently per platform, or when you want to display system diagnostics.

Importing

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

Methods and properties

MethodDescription
os.platform()Operating system platform ('linux', 'darwin', 'win32', etc.)
os.arch()CPU architecture ('x64', 'arm64', etc.)
os.cpus()Array of objects with information about each logical CPU core
os.totalmem()Total system memory in bytes
os.freemem()Free system memory in bytes
os.homedir()Path to the current user’s home directory
os.hostname()Hostname of the operating system
os.uptime()System uptime in seconds
os.tmpdir()Default directory for temporary files
os.type()OS name ('Linux', 'Darwin', 'Windows_NT')
os.release()OS release version string
os.userInfo()Information about the current user
os.networkInterfaces()Network interfaces that have a network address
os.endianness()CPU endianness ('BE' or 'LE')
os.loadavg()1, 5, and 15 minute load averages (Unix only)
os.EOLEnd-of-line marker for the current OS (\n on Unix, \r\n on Windows)
os.constantsObject containing OS-level constants (signals, error codes)

Examples

import os from 'node:os';

console.log(os.platform());   // 'darwin'
console.log(os.arch());       // 'arm64'
console.log(os.cpus().length); // 8 (number of CPU cores)
console.log(os.freemem());    // 2147483648 (bytes)
console.log(os.totalmem());   // 17179869184 (bytes)
console.log(os.homedir());    // '/Users/yourname'
console.log(os.uptime());     // 123456 (seconds since last boot)

For full documentation, see https://nodejs.org/api/os.html .