Skip to content

os

Since v8.0.74

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()
Usage example
1
2
const os = require('os');
console.println(os.platform());

type()

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

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

release()

Returns kernel release/version string.

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

hostname()

Returns host name.

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

homedir()

Returns current user home directory.

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

tmpdir()

Returns the default temporary directory path.

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

endianness()

Returns CPU endianness: BE or LE.

Syntax
endianness()
Usage example
1
2
const os = require('os');
console.println(os.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()
Usage example
1
2
const os = require('os');
console.println(os.uptime() >= 0);

bootTime()

Returns system boot time as Unix timestamp.

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

loadavg()

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

Syntax
loadavg()
Usage example
1
2
3
const os = require('os');
const avg = os.loadavg();
console.println(Array.isArray(avg), avg.length);

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)
Usage example
1
2
3
const os = require('os');
console.println(os.cpuCounts(true));
console.println(os.cpuCounts(false));

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()
Usage example
1
2
3
const os = require('os');
const ifaces = os.networkInterfaces();
console.println(typeof ifaces);

hostInfo()

Returns host information object.

Common fields include:

  • hostname, uptime, bootTime, procs
  • os, platform, platformFamily, platformVersion
  • kernelVersion, kernelArch
  • virtualizationSystem, virtualizationRole, hostId
Syntax
hostInfo()
Usage example
1
2
3
const os = require('os');
const info = os.hostInfo();
console.println(typeof info.hostname, typeof info.uptime);

userInfo()

Returns current user information.

Returned fields include:

  • username, homedir, shell
  • uid, gid
Syntax
userInfo([options])
Usage example
1
2
3
const os = require('os');
const user = os.userInfo();
console.println(user.username, user.homedir);

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)
Usage example
1
2
3
const os = require('os');
const usage = os.diskUsage('.');
console.println(typeof usage.total, typeof usage.usedPercent);

diskIOCounters()

Returns disk I/O counters.

  • names omitted or empty: all devices
  • names specified: selected devices
Syntax
diskIOCounters([names])
Usage example
1
2
3
const os = require('os');
const counters = os.diskIOCounters();
console.println(typeof counters);

netProtoCounters()

Returns network protocol counters.

Syntax
netProtoCounters([proto])
Usage example
1
2
3
const os = require('os');
const counters = os.netProtoCounters();
console.println(typeof counters);

constants

An object that contains operating system related constants.

It currently provides the following child objects.

  • os.constants.signals
  • os.constants.priority
Usage example
1
2
3
4
const os = require('os');

console.println(typeof os.constants.signals.SIGINT);
console.println(typeof os.constants.priority.PRIORITY_NORMAL);

os.constants.signals

An object that provides signal names and their numeric values.

These constants use canonical Unix-style signal numbers for API compatibility. On Windows, not every numeric value maps to a distinct native signal behavior.

For example, it includes entries such as:

LiteralNumber
SIGHUP1
SIGINT2
SIGQUIT3
SIGABRT6
SIGKILL9
SIGUSR110
SIGSEGV11
SIGUSR212
SIGPIPE13
SIGALRM14
SIGTERM15

These values can be passed to process.kill() as numeric signals.

On Windows, SIGINT is treated specially. JSH attempts to deliver it as an interrupt-style console control event, which is the closest available equivalent. By contrast, SIGTERM, SIGQUIT, and SIGKILL behave as termination requests on Windows.

Usage example
1
2
3
4
const os = require('os');
const process = require('process');

process.kill(12345, os.constants.signals.SIGTERM);

Relationship With The process Signal API

os.constants.signals provides signal numbers, while the actual signal handling and delivery are performed by the process module.

  • receive a signal: process.on('SIGINT', handler)
  • send a signal: process.kill(pid, os.constants.signals.SIGTERM)

process.on() accepts case-insensitive signal names, but signal event names must still use the canonical SIG-prefixed form such as SIGINT or sigint. Bare aliases such as term are not signal listener names.

process.kill() is more permissive and accepts aliases such as term in addition to canonical names. By contrast, os.constants.signals exposes canonical constant names only.

On Windows, process.kill(pid, os.constants.signals.SIGINT) is best-effort and depends on Windows console routing rules.

Usage example
1
2
3
4
5
6
7
8
const os = require('os');
const process = require('process');

process.on('sigint', () => {
  console.println('caught');
});

process.kill(process.pid, os.constants.signals.SIGINT);

os.constants.priority

An object that defines process priority levels.

Main fields
ConstantMeaning
PRIORITY_LOWlow priority
PRIORITY_BELOW_NORMALbelow-normal priority
PRIORITY_NORMALnormal default priority
PRIORITY_ABOVE_NORMALabove-normal priority
PRIORITY_HIGHhigh priority
PRIORITY_HIGHESThighest priority
Usage example
1
2
3
const os = require('os');
console.println(os.constants.priority.PRIORITY_NORMAL);
console.println(os.constants.priority.PRIORITY_HIGH);
Last updated on