Web Programming TutorialsLearn How to Build OS Module Utility Modules in NodeJS

Learn How to Build OS Module Utility Modules in NodeJS

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.

// Command to import OS Module in Node.js.

var OS = require("os");

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.

// Command to import OS Module in Node.js.
var os = require("os");
//1. Use of os.arch () method.
console.log('OS Architecture is : ' + os.arch ());
//2. Use of os.cpus () method.
console.log('OS cpus is : ' + os.cpus ());
//3. Use of os.endianness () method.
console.log('OS Endianness of CPU is : ' + os.endianness());
//4. Use of os.freemem () method.
console.log('OS free memory  is : ' + os.freemem ());
//5. Use of os.hostname () method.
console.log('OS hostname is : ' + os.hostname ());
//6. Use of os.loadavg () method.
console.log('OS load average array  is : ' + os.loadavg ());
//7. Use of os.platform () method.
console.log('OS platform is : ' + os.platform ());
//8. Use of os.networkInterfaces () method.
console.log('List of network Interfaces are : ' + os.networkInterfaces ());
//9. Use of os.release () method.
console.log('OS release is : ' + os.release ());
//10. Use of os.tmpdir () method.
console.log('OS tmp dir for files is : ' + os.tmpdir ());
//11. Use of os.totalmem () method.
console.log('OS total memory is : ' + os.totalmem ());
//12. Use of os.type () method.
console.log('OS type is : ' + os.type ());
//13. Use of os.uptime () method.
console.log('OS uptime is : ' + os.uptime ());

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.

OS Architecture is : x64
OS cpus is : [object Object],[object Object]
OS Endianness of CPU is : LE
OS free memory  is : 536956928
OS hostname is : APARAJITA-PC
OS load average array  is : 0,0,0
OS platform is : win32
List of network Interfaces are : [object Object]
OS release is : 10.0.14393
OS tmp dir for files is : C:\Users\APARAJ~1\AppData\Local\Temp
OS total memory is : 3726913536
OS type is : Windows_NT
OS uptime is : 11580.5427456

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -