Module basic module

Module os

osA module is one of the core modules used to run operating system functions. It provides utility functions for interacting with the operating system, including file address, file path, network interface, host name, operating system type, etc.

Common methods

osThere are many methods provided in the module. The following are some of the more commonly used methods:

os.hostname()

Get the hostname of the current computer.

Sample code:

1 2 3
const os = require('os'); const hostname = os.hostname(); console.log(hostname);

The return result is similar to the following:

1
localhost

os.type()

Get the name of the current operating system.

Sample code:

1 2 3
const os = require('os'); const type = os.type(); console.log(type);

The return result is similar to the following:

1
Windows_NT

os.release()

Get the current operating system version.

Sample code:

1 2 3
const os = require('os'); const release = os.release(); console.log(release);

The return result is similar to the following:

1
10.0.18362

os.arch()

Gets the processor architecture of the operating system.

Sample code:

1 2 3
const os = require('os'); const arch = os.arch(); console.log(arch);

The return result is similar to the following:

1
x64

os.cpus()

Get CPU information.

Sample code:

1 2 3
const os = require('os'); const cpus = os.cpus(); console.log(cpus);

The return result is similar to the following:

1 2 3 4 5 6
[ { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 2592, times: { user: 2400298, nice: 0, sys: 9684894, idle: 91516801, irq: 0 } }, { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 2592, times: { user: 464927, nice: 0, sys: 1454926, idle: 95119061, irq: 0 } }, { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 2592, times: { user: 232077, nice: 0, sys: 898942, idle: 95482112, irq: 0 } }, { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 2592, times: { user: 950448, nice: 0, sys: 1875169, idle: 93117788, irq: 0 } } ]

object

Service

ServiceConstructor, seeService

1
Service os.Service;

static function

hostname

Query the host name of the current running environment

1
static String os.hostname();

Return results:

  • String, returns the host name

endianness

Query the byte order of the current CPU

1
static String os.endianness();

Return results:

  • String, returns the byte order

type

Query the operating system name of the current running environment

1
static String os.type();

Return results:

  • String, returns the system name

release

Query the operating system version of the current running environment

1
static String os.release();

Return results:

  • String, return version information

homedir

Query the current user directory

1
static String os.homedir();

Return results:

  • String, returns directory string

arch

Query the current cpu environment

1
static String os.arch();

Return results:

  • String, returns the cpu type, possible results are 'amd64', 'arm', 'arm64', 'ia32'

loadavg

Query the average load of the running environment in 1 minute, 5 minutes, and 15 minutes

1
static Array os.loadavg();

Return results:

  • Array, returns an array containing three load data

totalmem

Query the total memory of the running environment, in bytes

1
static Long os.totalmem();

Return results:

  • Long, return memory data

freemem

Query the available memory of the running environment, in bytes

1
static Long os.freemem();

Return results:

  • Long, return memory data

cpus

Query the number and parameters of CPUs in the current running environment

1
static Array os.cpus();

Return results:

  • Array, returns an array containing cpu parameters, each item corresponds to a cpu

cpuNumbers

Query the number of CPUs in the current running environment

1
static Integer os.cpuNumbers();

Return results:

  • Integer, returns the number of cpu

tmpdir

Query the temporary file directory of the current running environment

1
static String os.tmpdir();

Return results:

  • String, return to the temporary file directory

userInfo

Returns the current valid execution user information

1
static Object os.userInfo(Object options = {});

Call parameters:

  • options: Object, the character encoding used to interpret the result string

Return results:

  • Object, currently valid execution user information

networkInterfaces

Query the current operating environment network information

1
static Object os.networkInterfaces();

Return results:

  • Object, return network card information

platform

Query the current platform name

1
static String os.platform();

Return results:

  • String, returns the platform name, possible results are 'darwin', 'freebsd', 'linux', or 'win32'

time

Parse a time string or query the current time of the running environment

1
static Date os.time(String tmString = "");

Call parameters:

  • tmString: String, time string, the default is to query the current time

Return results:

  • Date, returns a javascript Date object

dateAdd

Time calculation function, specify calculation time based on part

1 2 3
static Date os.dateAdd(Date d, Integer num, String part);

Call parameters:

  • d: Date, specifies the Date object used to calculate
  • num: Integer, specify the numerical value of the operation
  • part: String, specifies the time part of the operation, the received values ​​are: "year", "month", "day", "hour", "minute", "second"

Return results:

  • Date, returns a javascript Date object

static properties

timezone

Integer, query the current time zone of the running environment

1
static readonly Integer os.timezone;

EOL

String, query the line ending identifier of the current running environment, posix:\"\n\"; windows:\"\r\n\"

1
static readonly String os.EOL;