×

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
}

Related Topics

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Node.js REPL

Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems....

4 minutes read.

Node.js Error Handling

Node.js – Error Handling: Errors are part of every programming language, and Node.js Environment is not an exception. There are many predefined errors in Node.js, including errors like SyntaxError, RangeError, and...

4 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Difference Between Nodejs Angular and React

A Right JavaScript Framework Within a business, it is necessary to pick the right technology. As much as this applies to the web application and software development, it also applies in...

5 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Differentiation of Web Scraping and Web Crawling

Most of us get confused between the word’s web scraping and web crawling, and even some people think they are synonyms for each other. But there is a considerable difference between...

3 minutes read.

Node.js Event Loop

An Introduction to Event Loop As we have already discussed, Node.js is an Asynchronous platform with a non-blocking I/O. Node.js has awesome features that made it this thriving, and the Event...

5 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.

Node.js Parsing Arguments

Understanding Command-Line Arguments The Strings of text used to transfer extra information to a program through the command-line interface (CLI) while execution of the application, is known as Command-Line arguments. These...

9 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.