Saturday, April 18, 2020

Node.js OS Module

Node.js OS Module

Node.js os module


The os module is a built-in module in Node.js. This module provides underlying operating-system related information using various utility functions and properties. This module can be imported simply using require() function.

os.EOL

os.EOL is the end of the line sequence represented by \n on Linux and macOS, and \r\n on Windows.

os.constants

os.constants.signals provide all the constants related to handling process signals, like SIGHUP, SIGKILL and etc. 
os.constants.errno is used to set the constants related to the error reporting, like EADDRINUSE, EOVERFLOW etc.

os.arch()

This method returns the CPU architecture of the OS such as arm, x64, etc.

os.platform() 

This method returns information about the operating system's platform.

var os = require('os');
console.log("Platform: " + os.platform());
console.log("OS Architecture"+os.arch());
/*
Output:

Platform: win32

OS Architecturex64
*/

os.cpus()

This method returns an array having the information about all the CPU in the machine.

var os = require('os');
var arr=os.cpus();
for(item of arr)
{
     console.log(item);
}
/*
Output:

{

       model: 'Intel(R) Pentium(R) CPU  N3540  @ 2.16GHz',
       speed: 2167,
       times: { user: 898406, nice: 0, sys: 675234, idle: 8626375, irq: 37750 }
     }
     {
       model: 'Intel(R) Pentium(R) CPU  N3540  @ 2.16GHz',
       speed: 2167,
       times: { user: 1102593, nice: 0, sys: 686109, idle: 8410875, irq: 8796 }
     }
     {
       model: 'Intel(R) Pentium(R) CPU  N3540  @ 2.16GHz',
       speed: 2167,
       times: { user: 905421, nice: 0, sys: 612968, idle: 8681187, irq: 7187 }
     }
     {
       model: 'Intel(R) Pentium(R) CPU  N3540  @ 2.16GHz',
       speed: 2167,
       times: { user: 992781, nice: 0, sys: 723421, idle: 8483375, irq: 6265 }
     }
*/

os.endiannes()

This method returns the endianness depending on how Node was compiled BE or LE.

os.freemem()

This method returns the amount of free memory(bytes) of the system

os.hostname()

This method returns the hostname of the operating system

os.homedir() 

This method returns the home directory address


var os = require('os');
console.log(os.endianness());//Endianness
console.log(os.freemem()); //free memory available(bytes)
console.log(os.hostname());//host name
console.log(os.homedir());//home directory
/*
Output:

LE

376483840
hp
C:\Users\my
*/

os.networkInterfaces()

This method returns the network interfaces that have a network address

var os = require('os');
var ni=os.networkInterfaces();
for(item in ni)
console.log(ni[item]);//network interfaces
/*
Output:

[

  {
    address: '2401:4900:46a7:8590:55e1:f4d0:3cb8:8969',
    netmask: 'ffff:ffff:ffff:ffff::',
    family: 'IPv6',
    mac: '74:29:af:43:88:a5',
    internal: false,
    cidr: '2401:4900:46a7:8590:55e1:f4d0:3cb8:8969/64',
    scopeid: 0
  },
  {
    address: '2401:4900:46a7:8590:8df7:b298:f4e4:1751',
    netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
    family: 'IPv6',
    mac: '74:29:af:43:88:a5',
    internal: false,
    cidr: '2401:4900:46a7:8590:8df7:b298:f4e4:1751/128',
    scopeid: 0
  },
.
.
.
*/


os.loadavg().

This method returns an array containing the load averages, (1, 5, and 15 minutes) calculated by OS.


var os = require('os');
console.log(os.endianness());//Endianness
console.log(os.freemem()); //free memory available(bytes)
console.log(os.hostname());//host name
console.log(os.homedir());//home directory
/*
Output:

LE

376483840
hp
C:\Users\my
*/

os.release()

This method returns OS's release information.

os.tmpdir()

This method returns the operating system's default directory for temporary files

os.totalmem()

This method returns the number of total memory (bytes) of the system

os.type()

This method returns the name of the OS

os.uptime()

This method returns the uptime of the OS, in seconds

os.userInfo()

This method returns information about the current user


var os = require('os');
var avg=os.loadavg();
console.log("Average load->"+avg.toString());//CPU avg load
console.log("OS release->"+os.release());
console.log("Temp dir->"+os.tmpdir());
console.log("Total memory->"+os.totalmem());
console.log("OS type->"+os.type());
console.log("Uptime->"+os.uptime());
console.log("userInfo->");
console.log(os.userInfo());
/*
Output:

Average load->0,0,0

OS release->10.0.18362
Temp dir->C:\Users\my\AppData\Local\Temp
Total memory->4175708160
OS type->Windows_NT
Uptime->14469
userInfo->
{
  uid: -1,
  gid: -1,
  username: 'my',
  homedir: 'C:\\Users\\my',
  shell: null
}
*/

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