30 Days of node
Day 26 : OS Module in node.js






30 days of node - Nodejs tutorial series
Introduction

OS module in node.js is used to provide utilities related to operating system. It can be accessed using the following :

								
var os = require('os');
								
							

os.cpus()

This method is used to get the information of all the CPU/Core installed in the system. This method returns an array . Code snippet is given below :

								
//Name of the file : cpus.js
var os = require('os');
var value =  os.cpus();
console.log("os.cpus() ==> " + JSON.stringify(value) );
								
							

We can run it in the following way :
								
>node cpus.js
os.cpus() ==> [{"model":"Intel(R) Core(TM) i3-2348M CPU @ 2.30GHz","speed":2294,
"times":{"user":3067572,"nice":0,"sys":1015862,"idle":12850535,"irq":149667}},{"
model":"Intel(R) Core(TM) i3-2348M CPU @ 2.30GHz","speed":2294,"times":{"user":5
95611,"nice":0,"sys":169822,"idle":16167943,"irq":4664}},{"model":"Intel(R) Core
(TM) i3-2348M CPU @ 2.30GHz","speed":2294,"times":{"user":3278049,"nice":0,"sys"
:813467,"idle":12841783,"irq":13572}},{"model":"Intel(R) Core(TM) i3-2348M CPU @
 2.30GHz","speed":2294,"times":{"user":319786,"nice":0,"sys":80215,"idle":165331
57,"irq":967}}]
								
							

Note that In the above Output the value of nice is 0 because nice values are unix specified and our outputs are as per windows operating system.

os.arch()

This method is used to return the CPU architecture of operating system for which the node.js binary was compiled.
Possible values are : 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64', and 'x86'. This method returns String . Code snippet is given below :

								
//Name of the file: arch.js
var os = require('os');
var value =  os.arch();
console.log("os.arch() ==> " + value);
								
							

We can run it in the following way :
								
>node arch.js
os.arch() ==> ia32
								
							

os.endianness()

This method is used to get the endianness of the CPU for which the node.js binary was compiled.
Possible values are :

  • 'BE' : for big endian
  • 'LE' : for little endian
This method returns String . Code snippet is given below :
								
//Name of the file: endianness.js
var os = require('os');
var value =  os.endianness();
console.log("os.endianness() => " + value);
								
							

We can run it in the following way :
								
>node endianness.js
os.endianness() => LE
								
							

os.freemem()

This method is used to get the amount of free system memory in bytes. This method returns Integer . Code snippet is given below :

								
//Name of the file: freemem.js
var os = require('os');
var value =  os.freemem();
console.log("os.freemem() => " + value);
								
							

We can run it in the following way :
								
>node freemem.js
os.freemem() => 690982912
								
							

os.hostname()

This method is used to get the hostname of the operating system. This method returns String . Code snippet is given below :

								
//Name of the file: hostname.js
var os = require('os');
var value =  os.hostname();
console.log("os.hostname() => " + value);
								
							

We can run it in the following way :
								
>node hostname.js
os.hostname() => acer-PC
								
							

os.homedir()

This method is used to get the home directory of the current user. This method returns String . Code snippet is given below :

								
//Name of the file: homedir.js
var os = require('os');
var value =  os.homedir();
console.log("os.homedir() => " + value);
								
							

We can run it in the following way :
								
>node homedir.js
os.homedir() => C:\Users\acer
								
							

os.platform()

This method is used to get the operating system platform as set during compile time of node.js .
Possible values are :

  • 'win32'
  • 'aix'
  • 'freebsd'
  • 'linux'
  • 'openbsd'
  • 'darwin'
  • 'sunos'
This method returns String . Code snippet is given below :
								
//Name of the file: platform.js
var os = require('os');
var value =  os.platform();
console.log("os.platform() => " + value);
								
							

We can run it in the following way :
								
>node platform.js
os.platform() => win32
								
							

Note that the output of the above code will identify operating system platform as set during compile time of node.js .

os.release()

This method is used to get the operating system release.
This method returns String . Code snippet is given below :

								
//Name of the file: release.js
var os = require('os');
var value =  os.release();
console.log("os.release() => " + value);
								
							

We can run it in the following way :
								
>node release.js
os.release() => 6.1.7600
								
							

os.tmpdir()

This method is used to get the default directory for temporary files in operating system.
This method returns String . Code snippet is given below :

								
//Name of the file: tmpdir.js
var os = require('os');
var value =  os.tmpdir();
console.log("os.tmpdir() => " + value);
								
							

We can run it in the following way :
								
>node tmpdir.js
os.tmpdir() => C:\Users\acer\AppData\Local\Temp
								
							

os.totalmem()

This method is used to get the total amount of system memory in bytes.
This method returns Integer . Code snippet is given below :

								
//Name of the file: totalmem.js
var os = require('os');
var value =  os.totalmem();
console.log("os.totalmem() => " + value);
								
							

We can run it in the following way :
								
>node totalmem.js
os.totalmem() => 1948631040
								
							

os.uptime()

This method is used to get the system uptime in seconds.
This method returns Integer . Code snippet is given below :

								
//Name of the file: uptime.js
var os = require('os');
var value =  os.uptime();
console.log("os.uptime() => " + value);
								
							

We can run it in the following way :
								
>node uptime.js
os.uptime() => 22906.6507529
								
							

os.type()

This method is used to get the operating system name as returned by Uname.
Common values are :

  • 'Windows_NT' : on Windows
  • 'Linux' : on Linux
  • 'debian' : on MacOS
This method returns String . Code snippet is given below :
								
//Name of the file: type.js
var os = require('os');
var value =  os.type();
console.log("os.type() => " + value);
								
							

We can run it in the following way :
								
>node type.js
os.type() => Windows_NT
								
							

Summary

In this chapter of 30 days of node tutorial series, we learned about os module in node.js which is used to provide utilities related to operating system. We learned about following methods : os.cpus() , os.arch() , os.endianness() , os.freemem() , os.hostname() , os.homedir() , os.platform() , os.release() , os.tmpdir() , os.totalmem() , os.uptime() , os.type() , along with their code snippets .