os module

Updated March 13, 2026 · Node.js Modules
node os system platform environment

The os module is a core Node.js module that provides methods for retrieving information about the operating system, CPU, memory, and network interfaces. It is useful for building system monitoring tools, configuring application behavior based on the platform, and gathering diagnostic information.

Syntax

const os = require('os');

No arguments are required. The module is loaded synchronously.

Platform Information

os.platform()

Returns the operating system platform.

console.log(os.platform());
// 'linux', 'darwin', 'win32'

os.type()

Returns the operating system name.

console.log(os.type());
// 'Linux', 'Darwin', 'Windows_NT'

os.release()

Returns the operating system release version.

console.log(os.release());
// '5.4.0-1087-aws'

os.arch()

Returns the CPU architecture.

console.log(os.arch());
// 'x64', 'arm64', 'arm'

System Memory

os.totalmem()

Returns the total amount of system memory in bytes.

const total = os.totalmem();
console.log(`Total memory: ${(total / 1024 / 1024 / 1024).toFixed(2)} GB`);
// Total memory: 16.00 GB

os.freemem()

Returns the amount of free system memory in bytes.

const free = os.freemem();
console.log(`Free memory: ${(free / 1024 / 1024 / 1024).toFixed(2)} GB`);
// Free memory: 8.50 GB

os.freemem() percentage

Calculate available memory as a percentage.

const total = os.totalmem();
const free = os.freemem();
const percentFree = (free / total * 100).toFixed(1);
console.log(`Free: ${percentFree}%`);
// Free: 53.1%

CPU Information

os.cpus()

Returns an array of objects containing information about each CPU core.

console.log(os.cpus().length);
// 8

const cpus = os.cpus();
cpus.forEach((cpu, index) => {
  console.log(`Core ${index}: ${cpu.model}`);
  console.log(`  Speed: ${cpu.speed} MHz`);
});
// Core 0: Intel(R) Xeon(R) Platinum...
//   Speed: 2500 MHz

os.loadavg()

Returns an array containing the 1, 5, and 15 minute load averages.

console.log(os.loadavg());
// [0.5, 0.3, 0.2]

Note: This is only meaningful on Unix-like systems. On Windows, it returns [0, 0, 0].

Network Interfaces

os.networkInterfaces()

Returns an object containing network interfaces.

const interfaces = os.networkInterfaces();
console.log(Object.keys(interfaces));
// ['lo', 'eth0', 'docker0']

const eth0 = interfaces.eth0;
if (eth0) {
  eth0.forEach(info => {
    console.log(`${info.family}: ${info.address}`);
  });
}
// IPv4: 10.0.1.100
// IPv6: fe80::1

User Info

os.userInfo()

Returns information about the current user.

const user = os.userInfo();
console.log(user);
// {
//   uid: 1000,
//   gid: 1000,
//   username: 'node',
//   homedir: '/home/node',
//   shell: '/bin/bash'
// }

Common Patterns

Build system-specific paths

const path = require('path');
const homedir = os.homedir();
const configDir = path.join(homedir, '.myapp');

Monitor system resources

setInterval(() => {
  const free = os.freemem();
  const total = os.totalmem();
  const used = total - free;
  console.log(`Memory: ${(used / total * 100).toFixed(1)}%`);
}, 5000);

Detect development vs production environment

const isProduction = os.platform() === 'win32' 
  ? process.env.NODE_ENV === 'production'
  : process.env.NODE_ENV === 'production';

See Also

  • Fs:: — file system operations
  • Process:: — process-level information
  • Path:: — path manipulation