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:

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.
  • The OS module has also defined some properties that return all the constants related to handling process signals, error reporting, and many more.

 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 

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

The Output will look as follows:

Pin It on Pinterest

Share This