Learn How to Build OS Module Utility Modules in NodeJS

1
3928
OS Module Utility

In this article, we are going to discuss building an OS module utility by using one of the module present in the Node.js modules library. There are several modules present in Node.js library. They inlcude OS module, path modules, net module, DNS module, and domain module. All of these modules are very popular and used very commonly to build any Node based application.

Node.js OS Module
In Node.js, the OS module provides basic operating-system related utility functions. We can import this module by using the following syntax.

Methods present in the OS Module of node.js
The following are the basic methods and their descriptions which are present in the OS Module of Node.js.

S No

Method

Description

1.

os.arch ()

This method of the OS module is used to return the operating system CPU architecture. The possible values which are returned by this method are “arm”, “x64”, and “ia32”.

2.

os.cpus ()

This method of the OS module is used to return an array of objects that contains information about each CPU/core installed: model, speed (in terms of MHz), and times (It is an object that contains the number of milliseconds the CPU/core spent in: sys, user, nice, idle, and irq).

3.

os.endianness ()

This method of the OS module is used to return the endianness of the CPU. The possible values which are returned by this method are “BE” or “LE”.

4.

os.freemem ()

This method of the OS module is used to return the amount of free system memory in bytes.

5.

os.hostname ()

This method of the OS module is used to return the hostname of the operating system.

6.

os.loadavg ()

This method of the OS module is used to return an array that contains the 1, 5, and 15 minute load averages.

7.

os.platform ()

This method of the OS module is used to return the operating system platform.

8.

os.networkInterfaces ()

This method of the OS module is used to get a list of network interfaces.

9.

os.release ()

This method of the OS module is used to return the operating system release.

10.

os.tmpdir ()

This method of the OS module is used to return the operating system’s default directory for temp files.

11.

os.totalmem ()

This method of the OS module is used to return the total amount of system memory in bytes.

12.

os.type ()

This method of the OS module is used to return the operating system name.

13.

os.uptime ()

This method of the OS module is used to return the system uptime in seconds.

Properties present in the OS Module of node.js
The following is the basic property and its description which is present in the OS Module of Node.js.

S No

Property

Description

1.

os.EOL

It is a constant that defines the appropriate End-of-line marker for the operating system.

Example on use of OS Module in Node.js Application
In the following example, we are going to demonstrate the use of the above OS module methods. Like any other Node.js application, here we are going to create a js file named OSModule.js which has the following code.

Explanation of the code
In the above code, we have imported “os” module of the Node.js and used the following 13 methods to return the OS related values.

  • The 1st method [‘os.arch ()’] is used to return the OS architecture as “x64”.
  • The 2nd method [‘os.cpus ()’] is used to return an array of objects that contains information about each CPU/core installed.
  • The 3rd method [‘os.endianness ()’] is used to return endianness of the CPU as “LE”.
  • The 4th method [‘os.freemem ()’] is used to return the free memory of the OS as “536956928”.
  • The 5th method [‘os.hostname ()’] is used to return the hostname of the OS as “APARAJITA-PC”.
  • The 6th method [‘os.loadavg ()’] is used to return the OS load average array as [0, 0, 0].
  • The 7th method [‘os.platform ()’] is used to return the OS platform as “win32”.
  • The 8th method [‘os.networkInterfaces ()’] is used to return a list of network interfaces.
  • The 9th method [‘os.release ()’] is used to return the OS release as “10.0.14393”.
  • The 10th method [‘os.tmpdir ()’] is used to return the OS temp directory as “C:\Users\APARAJ~1\AppData\Local\Temp”.
  • The 11th method [‘os.totalmem ()’] is used to return the OS total memory as “3726913536”.
  • The 12th method [‘os.type ()’] is used to return the OS type as “Windows_NT”.
  • The 13th method [‘os.uptime ()’] is used to return the OS up time in seconds as “11580.5427456”.

Output
When we execute the above Node.js code then we will observe the following output on the console.

Source code for the OS Module Utility Modules in NodeJS

Conclusion: –
In this article, we discussed the various methods present in the OS module of Node.js. We have imported the OS module and use the associated methods to build an OS module Utility.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here