Nodejs Os Module
[Node.js Built-in Modules](#)
* * *
The `os` module in Node.js is a built-in core module that provides useful methods and properties related to the operating system. Through this module, we can obtain various information about the operating system running Node.js, such as CPU architecture, memory usage, network interfaces, etc.
### Why Do We Need the os Module
When developing applications, sometimes we need to perform different operations based on different operating system environments, or we need to monitor the system's resource usage. The `os` module provides us with such capability, allowing us to easily obtain these system information.
* * *
## How to Use the os Module
To use the `os` module, you first need to import it:
const os = require('os');
### Common Methods Introduction
#### 1. Get Operating System Information
## Example
console.log(os.platform());// Returns the operating system platform, e.g., 'darwin', 'win32', 'linux'
console.log(os.type());// Returns the operating system name, e.g., 'Linux', 'Darwin', 'Windows_NT'
console.log(os.arch());// Returns the CPU architecture, e.g., 'x64', 'arm'
console.log(os.release());// Returns the operating system version
#### 2. Get Memory Information
## Example
console.log(os.totalmem());// Returns the total system memory in bytes
console.log(os.freemem());// Returns the available system 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());// Returns an array of objects containing information about each CPU/core
console.log(os.loadavg());// Returns the average load for 1, 5, and 15 minutes (Linux/Unix only)
// Get CPU core count
console.log(`CPU Core Count: ${os.cpus().length}`);
#### 4. Get Network Interface Information
## Example
console.log(os.networkInterfaces());// Returns network interface information
#### 5. Get User Related Information
## Example
console.log(os.homedir());// Returns the home directory of the current user
console.log(os.userInfo());// Returns information about the current user
console.log(os.hostname());// Returns the host name of the operating system
* * *
## 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 Core Count: ${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 Network IP Address
## Example
const os = require('os');
function getLocalIP(){
const interfaces = os.networkInterfaces();
for(const interfaceName in interfaces){
for(const iface of interfaces){
if(iface.family==='IPv4'&&!iface.internal){
return iface.address;
}
}
}
return'127.0.0.1';
}
console.log(`Local IP Address: ${getLocalIP()}`);
* * *
## Notes
1. The information provided by the `os` module is read-only and cannot be used to modify system configuration.
2. The information returned may vary between different 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, which usually needs to be converted to more readable units (such as MB or GB).
* * *
## Methods
Common methods and properties of os are as follows:
| Method | Description | Example |
| --- | --- | --- |
| `os.arch()` | Returns the CPU architecture of the operating system. | `console.log(os.arch()); // Output: '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, usage, etc. | `console.log(os.cpus());` |
| `os.endianness()` | Returns the byte order of the CPU (`BE` for big-endian, `LE` for little-endian). | `console.log(os.endianness()); // Output: 'LE'` |
| `os.freemem()` | Returns the amount of free system memory in bytes, which can be used for monitoring system resources. | `console.log(os.freemem());` |
| `os.homedir()` | Returns the home directory path of the current user. | `console.log(os.homedir());` |
| `os.hostname()` | Returns the host name. | `console.log(os.hostname());` |
| `os.loadavg()` | Returns an array containing the average load for 1, 5, and 15 minutes, only valid on Unix systems. | `console.log(os.loadavg());` |
| `os.networkInterfaces()` | Returns an object containing address information for each network interface, such as `IP address`, `MAC address`. | `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 default temporary file directory path of the operating system. | `console.log(os.tmpdir());` |
| `os.totalmem()` | Returns the total system memory in bytes. | `console.log(os.totalmem());` |
| `os.type()` | Returns the name of the operating system, such as `'Linux'`, `'Darwin'` (macOS), `'Windows_NT'`, etc. | `console.log(os.type());` |
| `os.uptime()` | Returns the operating system uptime in seconds. | `console.log(os.uptime());` |
| `os.userInfo()` | Returns detailed information about the current user, such as username, home directory, UID, GID, etc. Supports configuring the options object to set character encoding (default is `'utf8'`). | `console
YouTip