OS Module

OS module in nodejs provides utilities related to operating system in your computer, to access these functions use require('os') for importing os module.

This module contains functions that are helpful in obtaining operating system related information.

For instance, you can get OS name, OS architecture, the total amount of OS memory, the amount of free memory etc

The output of the these functions depends upon the operating system on which the code is running, almost all of these functions return string

Function in OS module

arch()

arch() function 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'. arch() function returns String .



var os = require('os');
var value =  os.arch();
console.log(value);

constants

It is a big list, so click here to go official page https://nodejs.org/dist/latest-v8.x/docs/api/os.html#os_os_constants_1

cpus()

cpus() function is used to get the information of all the CPU/Core installed in the system. cpus() function returns an array .



var os = require('os');
var value =  os.cpus();
console.log(value);

endianness()

endianness() function is used to get the endianness of the CPU for which the node.js binary was compiled, returns string

'BE' : for big endian
'LE' : for little endian


var os = require('os');
var value =  os.endianness();
console.log(value);

freemem()

freemem() function is used to get the amount of free system memory in bytes. freemem() function returns Integer .


var os = require('os');
var value =  os.freemem();
console.log(value);

getPriority([pid])

The os.getPriority() method returns the scheduling priority for the process specified by pid. If pid is not provided, or is 0, the priority of the current process is returned.


var os = require('os');
var value =  os.getPriority();
console.log(value);

homedir()

homedir() function is used to get the home directory of the current user. homedir() function returns String .


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

hostname()

hostname() function is used to get the hostname of the operating system. hostname() function returns String .


var os = require('os');
var value =  os.hostname();
console.log(value);

platform()

platform() function in nodejs is used to get the operating system platform as set during compile time of node.js .

  • 'win32'
  • 'aix'
  • 'freebsd'
  • 'linux'
  • 'openbsd'
  • 'darwin'
  • 'sunos'

var os = require('os');
var value =  os.platform();
console.log(value);

release()

release() function is used to get the operating system release with nodejs


var os = require('os');
var value =  os.release();
console.log(value);

setPriority([pid, ]priority)

The os.setPriority() method attempts to set the scheduling priority for the process specified by pid. If pid is not provided, or is 0, the priority of the current process is used.

The priority input must be an integer between -20 (high priority) and 19 (low priority). Due to differences between Unix priority levels and Windows priority classes, priority is mapped to one of six priority constants in os.constants.priority. When retrieving a process priority level, this range mapping may cause the return value to be slightly different on Windows. To avoid confusion, it is recommended to set priority to one of the priority constants.

On Windows setting priority to PRIORITY_HIGHEST requires elevated user, otherwise the set priority will be silently reduced to PRIORITY_HIGH.

pid <integer> The process ID to set scheduling priority for. Default 0.
priority <integer> The scheduling priority to assign to the process.

tmpdir()

tmpdir() function is used to get the default directory for temporary files in operating system.


var os = require('os');
var value =  os.tmpdir();
console.log(value);

totalmem()

totalmem() function is used to get the total amount of system memory in bytes.


var os = require('os');
var value =  os.totalmem();
console.log(value);

type()

The os.type() method returns a string identifying the operating system name as returned by uname(3). For example, 'Linux' on Linux, 'Darwin' on macOS, and 'Windows_NT' on Windows.


var os = require('os');
var value =  os.type();
console.log(value);

uptime()

uptime() function is used to get the system uptime in seconds.


var os = require('os');
var value =  os.uptime();
console.log(value);

userInfo([options])

The os.userInfo() method returns information about the currently effective user, The returned object includes the username, uid, gid, shell, and homedir. On Windows, the uid and gid fields are -1, and shell is null.


var os = require('os');
// get only user name
var value =  os.userInfo("username");
console.log(value);
//get total user info
console.log(os.userInfo());

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group