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');
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) );
>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}}]
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);
>node arch.js
os.arch() ==> ia32
This method is used to get the endianness of the CPU for which the node.js binary was compiled.
Possible values are :
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);
>node endianness.js
os.endianness() => LE
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);
>node freemem.js
os.freemem() => 690982912
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);
>node hostname.js
os.hostname() => acer-PC
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);
>node homedir.js
os.homedir() => C:\Users\acer
This method is used to get the operating system platform as set during compile time of node.js
.
Possible values are :
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);
>node platform.js
os.platform() => win32
node.js
.
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);
>node release.js
os.release() => 6.1.7600
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);
>node tmpdir.js
os.tmpdir() => C:\Users\acer\AppData\Local\Temp
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);
>node totalmem.js
os.totalmem() => 1948631040
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);
>node uptime.js
os.uptime() => 22906.6507529
This method is used to get the operating system name as returned by Uname.
Common values are :
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);
>node type.js
os.type() => Windows_NT
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 .