os module in Node JS
The os
module in Node.js provides operating system-related utility methods and properties. It is a core module, meaning it comes bundled with Node.js and doesn't require separate installation. The os
module helps you interact with the underlying operating system in a platform-independent way, making it useful for tasks such as retrieving system information or managing system-specific settings.
Key Features of the os
Module
- System Information: Retrieve information about the operating system, such as hostname, platform, and architecture.
- System Resources: Access details about system memory, CPU usage, and more.
- Temporary Directory: Get the path to the system's temporary directory.
- Network Interfaces: List network interfaces and their addresses.
Commonly Used Methods and Properties
**1. os.platform()
Returns a string identifying the operating system platform.
Example:
const os = require('os');
console.log(os.platform()); // 'linux', 'darwin', 'win32', etc.
**2. os.type()
Returns the operating system name as a string.
Example:
const os = require('os');
console.log(os.type()); // 'Linux', 'Darwin', 'Windows_NT'
**3. os.arch()
Returns the CPU architecture of the operating system.
Example:
const os = require('os');
console.log(os.arch()); // 'x64', 'arm', etc.
**4. os.release()
Returns the operating system version.
Example:
const os = require('os');
console.log(os.release()); // '5.10.0-9-amd64', '20.4.0', etc.
**5. os.hostname()
Returns the hostname of the operating system.
Example:
const os = require('os');
console.log(os.hostname()); // 'my-computer'
**6. os.cpus()
Returns an array of objects containing information about each CPU/core installed.
Example:
const os = require('os');
const cpus = os.cpus();
cpus.forEach((cpu, index) => {
console.log(`CPU ${index}: ${cpu.model}`);
});
Output:
CPU 0: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
CPU 1: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
...
**7. os.totalmem()
Returns the total amount of system memory in bytes.
Example:
const os = require('os');
console.log(os.totalmem()); // 17179869184 (bytes)
**8. os.freemem()
Returns the amount of free system memory in bytes.
Example:
const os = require('os');
console.log(os.freemem()); // 8589934592 (bytes)
**9. os.uptime()
Returns the system uptime in seconds.
Example:
const os = require('os');
console.log(os.uptime()); // 123456 (seconds)
**10. os.tmpdir()
Returns the path to the system's temporary directory.
Example:
const os = require('os');
console.log(os.tmpdir()); // '/tmp' on UNIX-like systems or 'C:\Users\<username>\AppData\Local\Temp' on Windows
**11. os.networkInterfaces()
Returns an object containing network interfaces and their addresses.
Example:
const os = require('os');
const networkInterfaces = os.networkInterfaces();
console.log(networkInterfaces);
/* Example Output:
{
lo: [
{ address: '127.0.0.1', family: 'IPv4', internal: true, mac: '00:00:00:00:00:00', netmask: '255.0.0.0' }
],
eth0: [
{ address: '192.168.1.100', family: 'IPv4', internal: false, mac: '00:11:22:33:44:55', netmask: '255.255.255.0' }
]
}
*/
Use Cases
- System Monitoring: Collect information about the operating system and hardware to monitor system performance and health.
- Cross-Platform Applications: Adjust application behavior based on the underlying operating system or architecture.
- Resource Management: Access system memory and CPU details to manage resource-intensive applications.
- Temporary Files: Use the system's temporary directory for storing temporary files and data.
Example Use Case
System Resource Report
You might create a simple script to log system resources:
const os = require('os');
console.log('Operating System Information:');
console.log(`Platform: ${os.platform()}`);
console.log(`Architecture: ${os.arch()}`);
console.log(`OS Version: ${os.release()}`);
console.log(`Hostname: ${os.hostname()}`);
console.log(`Total Memory: ${os.totalmem() / (1024 * 1024 * 1024)} GB`);
console.log(`Free Memory: ${os.freemem() / (1024 * 1024 * 1024)} GB`);
console.log(`Uptime: ${os.uptime()} seconds`);
console.log(`Temporary Directory: ${os.tmpdir()}`);
console.log('Network Interfaces:');
console.log(os.networkInterfaces());
Summary
- System Information: Methods like
os.platform()
,os.type()
,os.arch()
,os.release()
, andos.hostname()
provide details about the operating system. - System Resources: Methods such as
os.cpus()
,os.totalmem()
,os.freemem()
, andos.uptime()
give insights into system performance and resource usage. - Temporary Directory:
os.tmpdir()
provides the path to the system's temporary directory. - Network Interfaces:
os.networkInterfaces()
lists network interfaces and their addresses.