Skip to content

os

Since v8.0.73

The os module provides Node.js-compatible operating system information APIs for JSH applications.

arch()

Returns CPU architecture.

Syntax
arch()
Usage example
1
2
const os = require('os');
console.println(os.arch());

platform()

Returns platform name such as darwin, linux, or windows.

Syntax
platform()

type()

Returns OS type such as Darwin, Linux, or Windows_NT.

Syntax
type()

release()

Returns kernel release/version string.

Syntax
release()

hostname()

Returns host name.

Syntax
hostname()

homedir()

Returns current user home directory.

Syntax
homedir()

tmpdir()

Returns the default temporary directory path.

Syntax
tmpdir()

endianness()

Returns CPU endianness: BE or LE.

Syntax
endianness()

EOL

Platform-specific end-of-line marker.

  • POSIX: \n
  • Windows: \r\n
Usage example
1
2
const os = require('os');
console.println(JSON.stringify(os.EOL));

totalmem(), freemem()

Returns total/free memory in bytes.

Syntax
totalmem()
freemem()
Usage example
1
2
3
const os = require('os');
console.println('total:', os.totalmem());
console.println('free :', os.freemem());

uptime()

Returns system uptime in seconds.

Syntax
uptime()

bootTime()

Returns system boot time as Unix timestamp.

Syntax
bootTime()

loadavg()

Returns load averages as [1min, 5min, 15min].

Syntax
loadavg()

cpus()

Returns per-CPU information array.

Each item includes fields like:

  • model, speed, cores
  • vendor, family, model, stepping
  • times.user, times.nice, times.sys, times.idle, times.irq (milliseconds)
Syntax
cpus()
Usage example
1
2
3
const os = require('os');
const list = os.cpus();
console.println(Array.isArray(list), list.length > 0);

cpuCounts()

Returns CPU count.

  • true: logical CPU count
  • false: physical CPU count
Syntax
cpuCounts(logical)

cpuPercent()

Returns CPU usage percentages.

  • intervalSec: sampling interval in seconds (0 for immediate)
  • perCPU: return per-core values when true
Syntax
cpuPercent(intervalSec, perCPU)
Usage example
1
2
const os = require('os');
console.println(Array.isArray(os.cpuPercent(0, true)));

networkInterfaces()

Returns network interface information object.

The object shape is:

  • key: interface name
  • value: array of address objects
    • address
    • family (IPv4/IPv6)
    • internal (boolean)
Syntax
networkInterfaces()

hostInfo()

Returns host information object.

Common fields include:

  • hostname, uptime, bootTime, procs
  • os, platform, platformFamily, platformVersion
  • kernelVersion, kernelArch
  • virtualizationSystem, virtualizationRole, hostId
Syntax
hostInfo()

userInfo()

Returns current user information.

Returned fields include:

  • username, homedir, shell
  • uid, gid
Syntax
userInfo([options])

diskPartitions()

Returns disk partition list.

Syntax
diskPartitions([all])
Usage example
1
2
3
const os = require('os');
const parts = os.diskPartitions();
console.println(Array.isArray(parts), parts.length >= 0);

diskUsage()

Returns disk usage for the given path.

Typical fields include total, used, free, usedPercent.

Syntax
diskUsage(path)

diskIOCounters()

Returns disk I/O counters.

  • names omitted or empty: all devices
  • names specified: selected devices
Syntax
diskIOCounters([names])

netProtoCounters()

Returns network protocol counters.

Syntax
netProtoCounters([proto])

constants

OS constants object.

Main fields
  • constants.signals
    • SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM
  • constants.priority
    • PRIORITY_LOW, PRIORITY_BELOW_NORMAL, PRIORITY_NORMAL, PRIORITY_ABOVE_NORMAL, PRIORITY_HIGH, PRIORITY_HIGHEST
Usage example
1
2
3
const os = require('os');
console.println(os.constants.signals.SIGINT);
console.println(os.constants.priority.PRIORITY_NORMAL);
Last updated on