nodejs
  1. nodejs-os-module

Node.js OS Module

In Node.js, the os module provides a way of interacting with the operating system on which the Node.js process is running. It provides methods to access information about the operating system, as well as some basic system operations. In this tutorial, we'll explore the os module in Node.js.

Syntax

The os module can be accessed using the following syntax:

const os = require('os');

Example

Let's see a few examples of using the os module.

Getting System Information

We can use the os module to retrieve various information about the operating system, such as the number of CPUs, total system memory, and network interfaces. Here's an example code snippet:

const os = require('os');

console.log(`Number of CPUs: ${os.cpus().length}`);
console.log(`Total memory: ${(os.totalmem() / 1024 /1024 /1024).toFixed(2)} GB`);
console.log(`Current user: ${os.userInfo().username}`);
console.log(`Hostname: ${os.hostname()}`);
console.log(`Network interfaces: ${JSON.stringify(os.networkInterfaces())}`);

In this example, we've used various functions of the os module such as cpus(), totalmem(), userInfo(), hostname(), and networkInterfaces() to print system information.

Getting Platform Specific Information

We can also use the os module to retrieve platform-specific information about the operating system. Here's an example:

const os = require('os');

if (os.platform() === 'win32') {
  console.log('Windows');
} else if (os.platform() === 'darwin') {
  console.log('macOS');
} else {
  console.log('Linux');
}

In this example, we use the platform() method of the os module to determine the current operating system platform.

Output

When we run the example code of system information on a machine, the output will look like:

Number of CPUs: 8
Total memory: 15.94 GB
Current user: user
Hostname: machine-name
Network interfaces: {"Wi-Fi":[{"address":"192.168.1.10","netmask":"255.255.255.0","family":"IPv4","mac":"XX-XX-XX-XX-XX-XX","internal":false}],"Loopback Pseudo-Interface 1":[{"address":"::1","netmask":"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff","family":"IPv6","mac":"00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00","scopeid":0,"internal":true}]}

When we run the example code to get platform-specific information on a Linux system, the output will look like:

Linux

Explanation

In the examples above, we've used the os module of Node.js to retrieve various pieces of information about the operating system. We've used functions like cpus(), totalmem(), userInfo(), platform(), and networkInterfaces() to print system information such as the number of CPUs, total system memory, current user, hostname, network interfaces, and platform-specific information.

Use

The os module in Node.js is used to interact with the operating system on which the Node.js process is running. It provides various methods to access information about the operating system, as well as some basic system operations.

Important Points

  • The os module is a built-in module in Node.js and does not require any installation.
  • The os module provides various methods to access information about the operating system, such as CPU details, memory details, user information, network interfaces, and platform-specific information.
  • The os module is platform-specific. The functions and information provided by the module may vary depending on the operating system.

Summary

In this tutorial, we explored the os module in Node.js, which is used to interact with the operating system on which the Node.js process is running. We discussed the syntax, example code snippets, output, explanation, use, and important points of using the os module. With this knowledge, you can now use the os module in your Node.js applications to retrieve various system information and perform some basic system operations.

Published on: