NodeJS OS Module: A Complete Guide

NodeJS OS is used to get information regarding the OS installed on the machine in which Node is running. Node OS will help to interact with the Operation System running the Node.js server to manage the resources, performance, etc. It is also to get information regarding the CPU, architecture, operation system uptime, system’s constraints, total memory, free memory available, hostname, etc.

Suppose we are running a giant Node.js server on a machine, and we have to manage the server so that it won’t take the whole memory, if it takes complete memory, then there is a high chance that the server will crash. So we can use the Node OS to get the information about the total system memory and the free memory available and design the server in a way that releases some unused memory before it is full.

Not only this, there are various functionalities provided by the Node OS module, let’s see them one by one.

Node OS Module Methods

Before going further make sure you have Node installed so that you are able to run the given commands and examples code on your system. Type the below command in the terminal, if it returns any value it means Node.js is successfully installed.

arch()

This method is used to get information regarding the system architecture.

Example:

const os = require('os');

const output = os.arch();
console.log(output);

Output:

x64

cpus()

This method returns the available CPUs on the system.

Example:

const os = require('os');

const output = os.cpus()
console.log(output);

Output:

[
  {
    model: '11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz',
    speed: 2995,
    times: {
      user: 13165093,
      nice: 0,
      sys: 13549453,
      idle: 144999625,
      irq: 1201968
    }
  },
  {
    model: '11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz',
    speed: 2995,
    times: {
      user: 11404109,
      nice: 0,
      sys: 9191968,
      idle: 151117968,
      irq: 181578
    }
  }...

endianness()

This method returns the endianness of the CPU.

Example:

const os = require('os');

const output = os.endianness()
console.log(output);

Output:

LE.

freemem()

This method returns the free memory of the system.

Example:

const os = require('os');

const output = os.freemem()
console.log(output);

Output:

937365504

totalmem()

Returns the total memory of the system.

Example:

const os = require('os');

const output = os.totalmem()
console.log(output);

Output:

3974742016

hostname()

This method gives the hostname of the system.

Example:

const os = require('os');

const output = os.hostname()
console.log(output);

Output:

LAPTOP-4RHH6MFI

userInfo()

This method gives information regarding the currently logged user.

Example:

const os = require('os');

output = os.userInfo()
console.log(output);

Output:

{
  uid: -1,
  gid: -1,
  username: 'ag290',
  homedir: 'C:\\Users\\ag290',
  shell: null
}

platform()

Returns the platform of the system.

Example:

const os = require('os');

const output = os.platform()
console.log(output);

Output:

win32

release()

Returns the information regarding the release of the operating system.

Example:

const os = require('os');

const output = os.release()
console.log(output);

Output:

10.0.19044

type()

Return the types of the operating system.

Example:

const os = require('os');

const output = os.type()
console.log(output);

Output:

Windows_NT

networkInterfaces()

This method returns the network interface.

Example:

const os = require('os');

const output = os.networkInterfaces()
console.log(output);

Output:

{
  'Wi-Fi': [
    {
      ...
     }
]}

uptime()

Returns the system uptime.

Example:

const os = require('os');

const output = os.uptime()
console.log(output);

Output:

288139

Cloud System Specifications

We host our Node.js application on different types of cloud os, and the company charges us according to the uses and memory so we can also use this module to track the memory uses to decrease the cost, also you can double-check that the architect and CPUs, available memory are the same as mentioned in the plan or they are falsely claiming.

Summary

OS is a built-in module in Node.js used to interact with the operating system and gives different types of information like CPU architecture, currently logged users, total memory, free memory, and more. It is useful during the creation of a server on a system to know the details about the system for best performance. It also helps in memory management. Hope this article helps you to understand the Node OS.

Reference

https://nodejs.org/api/os.html#os

Aditya Gupta
Aditya Gupta
Articles: 161