OS Module
in Node.js



OS module provide utilities related to operating system. It can be accessed using the following :

						
const os = require('os');
						
					

os.cpus()

This method is used to get the information related to all the CPU/Core installed in the system. It returns an array.
We can use this in the following way :


		        
const os = require('os');

var value =  os.cpus();

console.log("os.cpus() ==> " + JSON.stringify(value) );
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And It will return an array. which contains information about all the cpu/core installed.


		        
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 : In the above Output the value of nice is 0 because nice values are unix specified and our outputs are as per windows.

os.arch()

This method is used to get the CPU architecture of operating system for which the node.js binary was compiled. It's return type is string.
Possible values are : 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64', and 'x86'.
We can use this in the following way :


		        
const os = require('os');

var value =  os.arch();

console.log("os.arch() ==> " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the CPU architecture of OS.


		        
os.arch() ==> ia32
				
	            

os.endianness()

This method is used to get the endianness of the CPU for which the node.js binary was compiled. It's return type is string.
Possible values are :

  • 'BE' : for big endian
  • 'LE' :for little endian
We can use this in the following way :


		        
const os = require('os');

var value =  os.endianness();

console.log("os.endianness() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the endianness of the CPU


		        
os.endianness() => LE
				
	            

os.freemem()

This method is used to get the amount of free system memory in bytes. It's return type is integer.
We can use this in the following way :


		        
const os = require('os');

var value =  os.freemem();

console.log("os.freemem() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the amount of free memory in bytes :


		        
os.freemem() => 690982912
				
	            

os.hostname()

This method is used to get the hostname of the operating system. It's return type is string.
We can use this in the following way :


		        
const os = require('os');

var value =  os.hostname();

console.log("os.hostname() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the endianness of the CPU


		        
os.hostname() => acer-PC
				
	            

os.homedir()

This method is used to get the home directory of the current user. It's return type is string.
We can use this in the following way :


		        
const os = require('os');

var value =  os.homedir();

console.log("os.homedir() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the home directory of the current user :


		        
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. It's return type is string.
Possible values are :

  • 'win32'
  • 'aix'
  • 'freebsd'
  • 'linux'
  • 'openbsd'
  • 'darwin'
  • 'sunos'
We can use this in the following way :


		        
const os = require('os');

var value =  os.platform();

console.log("os.platform() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

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


		        
os.platform() => win32
				
	            

os.release()

This method is used to get the operating system release. It's return type is string.
We can use this in the following way :


		        
const os = require('os');

var value =  os.release();

console.log("os.release() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the Operating System release :


		        
os.release() => 6.1.7600
				
	            

os.tmpdir()

This method is used to get the default directory for temporary files in operating system. It's return type is string.
We can use this in the following way :


		        
const os = require('os');

var value =  os.tmpdir();

console.log("os.tmpdir() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the default temp directory in the OS


		        
os.tmpdir() => C:\Users\acer\AppData\Local\Temp
				
	            

os.totalmem()

This method is used to get the total amount of system memory in bytes as integer. It's return type is integer.
We can use this in the following way :


		        
const os = require('os');

var value =  os.totalmem();

console.log("os.totalmem() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will return the total memory of system in bytes as an integer :


		        
os.totalmem() => 1948631040
				
	            

os.type()

This method is used to get the operating system name as returned by Uname . It's return type is string.
Common values are :

  • 'debian' : on MacOS
  • 'Windows_NT' : on Windows
  • 'Linux' : on Linux
We can use this in the following way :


		        
const os = require('os');

var value =  os.type();

console.log("os.type() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will identify the OS names


		        
os.type() => Windows_NT
				
	            

os.uptime()

This method is used to get the system uptime in seconds. It's return type is integer.
We can use this in the following way :


		        
const os = require('os');

var value =  os.uptime();

console.log("os.uptime() => " + value);
				
	            

you can run the code using the following command on bash.


		        
G:\nodejsera>node demo.js
				
	            

And the output of the above code will return the system uptime in seconds :


		        
os.uptime() => 22906.6507529