Node.js OS Module

Introduction to Node.js OS Module

The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating System's hostname and return the amount of free space available on the system in bytes.

Since the OS Module is a Build-in Module that comes when Node.js is installed, so we can call the module using the following syntax:

const os = require(‘os’);

Some fundamental properties of OS module

Some useful and fundamental properties of OS module provide us some key factors related to handling files:

  • The OS module has defined EOL constant that provides the line delimiter sequence. It returns the operating system specified end-of-line marker, which is \r\n on Windows, and \n on Linux.
os.EOL
  • The OS module has also defined some properties that return all the constants related to handling process signals, error reporting, and many more.
os.constants

 Main methods of Node.js OS module

There are various methods described in OS module of Node.js. These methods involve reading and interacting with current Operating System and fetch various details including memory, process and many more. Some of popular methods are listed below:

  1. os.type()
  2. os.arch()
  3. os.platform()
  4. os.release()
  5. os.version()
  6. os.cpus()
  7. os.networkInterfaces()
  8. os.loadavg()
  9. os.hostname()
  10. os.freemem()
  11. os.userinfo()

Let’s discuss each of them.

  • os.type()

The OS module provides the name of the Operating System using the os.type() method.

  • os.arch()

The OS module provides the CPU architecture of the current Operating System using the os.arch() method.

  • os.platform()

The OS module returns a string recognizing the native Operating System platform using theos.platform() method.

  • os.release()

The OS module also returns a string identifying the details of the Operating System's release with the os.release() method.

  • os.version()

The OS module delivers a string recognizing the kernel version using the os.version() method.

  • os.cpus()

The OS module returns an array of information about the CPUs available on one's system using the os.cpus() method.

  • os.networkInterfaces()

The OS module delivers the network interfaces available on the system using the os.networkinterface() method.

  • os.loadavg()

The OS module provides the calculation made by an Operating system on the load average using the os.loadavg() method. However, this method returns an expressive value on macOS and Linux only.

  • os.hostname()

The OS module returns the name of the host using the os.hostname() method.

  • os.freemem()

The OS module returns the free memory in the system with a representation of the number of bytes using the os.freemem() method.

  • os.userInfo()

The OS module also returns an instance containing the user's details, which include the username, uid, shell, gid and homedir using the os.userInfo() method.

Below is an example demonstrating the functioning of the above-stated methods:

File: os_module.js 

const os = require('os');
// some basic methods used in OS module
const my_system = {
    name: os.type(),
    architecture: os.arch(),
    platform: os.platform(),
    release: os.release(),
    version: os.version(),
    hostname: os.hostname()
};
console.log(my_system);
// printing the free memory of the system
console.log("The memory available in the system: " + os.freemem() + " bytes.");
// network interface of the system
console.log("Network Interface: ");
console.log(os.networkInterfaces());
// details of the CPUs
console.log("CPU Details: ");
console.log(os.cpus());
// User's Information
console.log("User Details: ");
console.log(os.userInfo());

To see the Output, let’s execute the program:

$ node os_module.js

The Output will look as follows:

{
  name: 'Windows_NT',      
  architecture: 'x64',     
  platform: 'win32',       
  release: '10.0.18363',   
  version: 'Windows 10 Pro',
  hostname: 'Mango-Home'   
}
The memory available in the system: 747491328 bytes.
Network Interface:
{
  Ethernet: [
    {
      address: 'fe80::15bb:8680:cc55:b9ac',
      netmask: 'ffff:ffff:ffff:ffff::',   
      family: 'IPv6',
      mac: 'fc:aa:14:fc:c4:0e',
      internal: false,
      cidr: 'fe80::15bb:8680:cc55:b9ac/64',
      scopeid: 4
    },
    {
      address: '192.168.0.104',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: 'fc:aa:14:fc:c4:0e',
      internal: false,
      cidr: '192.168.0.104/24'
    }
  ],
  'Loopback Pseudo-Interface 1': [
    {
      address: '::1',
      netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
      family: 'IPv6',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '::1/128',
      scopeid: 0
    },
    {
      address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: '00:00:00:00:00:00',
      internal: true,                         
      cidr: '127.0.0.1/8'
    }
  ]
}                                                     
CPU Details:
[
  {
    model: 'Intel(R) Pentium(R) CPU G2010 @ 2.80GHz',
    speed: 2794,
    times: {
      user: 1999234,
      nice: 0,
      sys: 1350312,
      idle: 13303187,
      irq: 124437
    }
  },
  {
    model: 'Intel(R) Pentium(R) CPU G2010 @ 2.80GHz',
    speed: 2794,
    times: {
      user: 2224062,
      nice: 0,
      sys: 1205531,
      idle: 13222875,
      irq: 17484
    }
  }
]
User Details:
{
  uid: -1,
  gid: -1,
  username: 'SUHAIL KHAN',
  homedir: 'C:\\Users\\SUHAIL KHAN',
  shell: null
}