Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js os Module

Node.js os Module

Java FileNode.js Built-in Modules


Node.js's os The module is a built-in core module that provides utility methods and properties related to the operating system. Through this module, we can obtain various information about the operating system on which Node.js is currently running, such as CPU architecture, memory usage, and network interfaces.

Why you need the os module

When developing applications, sometimes we need to perform different operations depending on the operating system environment, or we need to monitor the system's resource usage.os The module provides us with this capability, allowing us to easily obtain this system information.


How to use the os module

To use os module, you first need to import it:

const

os

=

require

(

'os'

)

;

Introduction to common methods

1. Get operating system information

Example

console.

log

(

os.

platform

(

)

)

;

 

// Return the operating system platform, such as 'darwin', 'win32', 'linux'

console.

log

(

os.

type

(

)

)

;

     

// Return the operating system name, such as 'Linux', 'Darwin', 'Windows_NT'

console.

log

(

os.

arch

(

)

)

;

     

// Return the CPU architecture, such as 'x64', 'arm'

console.

log

(

os.

release

(

)

)

;

 

// Return the operating system version

2. Get memory information

Example

console.

log

(

os.

totalmem

(

)

)

;

 

// Return the system's total memory (in bytes)

console.

log

(

os.

freemem

(

)

)

;

 

// Return the system's available memory (in bytes)

// Convert to MB for display

console.

log

(

`Total memory

:

$

{

os.

totalmem

(

)

/

1024

/

1024

}

MB`

)

;

console.

log

(

`Available memory

:

$

{

os.

freemem

(

)

/

1024

/

1024

}

MB`

)

;

3. Get CPU information

Example

console.

log

(

os.

cpus

(

)

)

;

     

// Return an array of objects containing information about each CPU/core

console.

log

(

os.

loadavg

(

)

)

;

 

// Return the 1, 5, and 15 minute load averages (Linux/Unix only)

// Get the number of CPU cores

console.

log

(

`CPU cores

:

$

{

os.

cpus

(

)

.

length

}

`

)

;

4. Get network interface information

Example

console.

log

(

os.

networkInterfaces

(

)

)

;

 

// Return network interface information

5. Get information about the current user

Example

console.

log

(

os.

homedir

(

)

)

;

 

// Return the current user's home directory

console.

log

(

os.

userInfo

(

)

)

;

 

// Return the current user's information

console.

log

(

os.

hostname

(

)

)

;

 

// Return the operating system's hostname


Practical Application Examples

Example 1: System monitoring tool

Example

const

os

=

require

(

'os'

)

;

function

systemMonitor

(

)

{

  console.

log

(

'=== System Monitoring ==='

)

;

  console.

log

(

`Operating system

:

$

{

os.

type

(

)

}

$

{

os.

release

(

)

}

`

)

;

  console.

log

(

`CPU architecture

:

$

{

os.

arch

(

)

}

`

)

;

  console.

log

(

`CPU cores

:

$

{

os.

cpus

(

)

.

length

}

`

)

;

  console.

log

(

`Total memory

:

$

{

(

os.

totalmem

(

)

/

1024

/

1024

)

.

toFixed

(

2

)

}

MB`

)

;

  console.

log

(

`Available memory

:

$

{

(

os.

freemem

(

)

/

1024

/

1024

)

.

toFixed

(

2

)

}

MB`

)

;

  console.

log

(

`System uptime

:

$

{

(

os.

uptime

(

)

/

60

/

60

)

.

toFixed

(

2

)

}

hours`

)

;

}

systemMonitor

(

)

;

Example 2: Get the network IP address

Example

const

os

=

require

(

'os'

)

;

function

getLocalIP

(

)

{

 

const

interfaces

=

os.

networkInterfaces

(

)

;

 

for

(

const

interfaceName

in

interfaces

)

{

   

for

(

const

iface of interfaces

[

interfaceName

]

)

{

     

if

(

iface.

family

===

'IPv4'

&&

!

iface.

internal

)

{

       

return

iface.

address

;

     

}

   

}

 

}

 

return

'127.0.0.1'

;

}

console.

log

(

`Local IP address

:

$

{

getLocalIP

(

)

}

`

)

;


Notes

  1. os The information provided by the module is read-only and cannot be used to modify system configuration.
  2. The information returned may vary between operating systems, especially between Windows and Unix-like systems.
  3. Some methods (such as loadavg()) may not be available on Windows or may return different values.
  4. Memory information is returned in bytes and usually needs to be converted to a more readable unit (such as MB or GB).

Method

Common os methods and properties are as follows:

Method Description Example
os.arch() Returns the CPU architecture of the operating system. console.log(os.arch()); // 输出:'x64'
os.constants Returns an object containing operating system-specific constants (such as error codes, signals, etc.). console.log(os.constants.signals);
os.cpus() Returns an array of objects, each containing information about a CPU core, such as model, speed, and usage. console.log(os.cpus());
os.endianness() Returns the CPU's byte order (BE indicates big-endian byte order,LE indicates little-endian byte order). console.log(os.endianness()); // 输出:'LE'
os.freemem() Returns the amount of free system memory (in bytes), which can be used to monitor system resources. console.log(os.freemem());
os.homedir() Returns the path to the current user's home directory. console.log(os.homedir());
os.hostname() Returns the hostname. console.log(os.hostname());
os.loadavg() Returns an array containing the 1, 5, and 15 minute load averages, valid only on Unix systems. console.log(os.loadavg());
os.networkInterfaces() Returns an object containing address information for each network interface, such as IP 地址、MAC 地址。 console.log(os.networkInterfaces());
os.platform() Returns the operating system platform, such as 'darwin'、'win32'、'linux' , etc. console.log(os.platform());
os.release() Returns the release version of the operating system. console.log(os.release());
os.tmpdir() Returns the path to the operating system's default temporary directory. console.log(os.tmpdir());
os.totalmem() Returns the total system memory (in bytes). console.log(os.totalmem());
os.type() Returns the operating system's name, such as 'Linux'、'Darwin'(macOS)、'Windows_NT' , etc. console.log(os.type());
os.uptime() Returns the operating system's uptime, in seconds. console.log(os.uptime());
os.userInfo([options]) Returns detailed information about the current user, such as username, home directory, UID, GID, etc. Supports an options object to set the character encoding (the default is 'utf8')。 console.log(os.userInfo());

property

property Description Example
os.EOL Returns the end-of-line identifier for the current operating system (\n or \r\n), making it easier to handle cross-platform file reading and writing. console.log(os.EOL); // Windows 输出:'\r\n',Linux 输出:'\n'

Example

Create a main.js file with the following code:

Example

const

os

=

require

(

'os'

)

;

// Get the system architecture

console.

log

(

'System architecture:'

,

os.

arch

(

)

)

;

// Get CPU information

console.

log

(

'CPU Information:'

,

os.

cpus

(

)

)

;

// Get free memory

console.

log

(

'Free memory:'

,

os.

freemem

(

)

)

;

// Get total memory

console.

log

(

'Total memory:'

,

os.

totalmem

(

)

)

;

// Get the home directory

console.

log

(

'Home directory:'

,

os.

homedir

(

)

)

;

// Get the system platform

console.

log

(

'System platform:'

,

os.

platform

(

)

)

;

// Get the system name

console.

log

(

'System name:'

,

os.

type

(

)

)

;

// Get the load average (Unix systems only)

console.

log

(

'Load average:'

,

os.

loadavg

(

)

)

;

// Get network interface information

console.

log

(

'Network interface information:'

,

os.

networkInterfaces

(

)

)

;

The code execution result is similar to the following:



系统架构: arm64

CPU 信息: [

  {

    model: 'Apple M1',

    speed: 24,

    times: { user: 18515240, nice: 0, sys: 9460280, idle: 38401220, irq: 0 }

  }

...

]

空闲内存: 167411712

总内存: 8589934592

主目录: /Users/RUNOOB

系统平台: darwin

系统名称: Darwin

平均负载: [ 11.4658203125, 4.49560546875, 3.35791015625 ]

网络接口信息: {

...

Node.js Utility ModulesNode.js Utility Modules

Java FileNode.js Built-in Modules