The Node.js OS module has many useful utility functions for getting information about the computer system that the OS module’s program is running on. It can get us information about hardware like CPUs, endianness, the home directory, IP address, hostname, the platform the program is running on, system uptime, information about the currently logged in user and more.
We can use the OS module by writing const os = require('os');
at the top of a JS file. There are many useful properties in the OS module. Below are more useful properties in the OS module:
os.release
The os.release
function returns a string that identifies the operating system release. On Unix and Linux systems, the function identifies the operating system with the uname
command. On Windows, the GetVersionExW()
function from the Win32 API is used. For example, if we run:
console.log(os.release());
Then we may get something like:
'4.15.0-1036-gcp'
os.setPriority
The os.setPriority
function lets us set the scheduling priority of a process specified by the process ID. It takes 2 arguments. The first is the process ID, which is a whole number that identifies the running process to set the priority to. The default value to 0.
The second argument is the priority, which is a whole number which indicates the scheduling of the process priority we want to set. It ranges from -20 for the highest priority to the lowest which is 19.
Because of the differences between priority levels between Windows and POSIX systems, Node.js provides us with constants that have the levels of priorities for processes.
They’re in the os.constants.priority
constant. On Windows, setting the process priority to the PRIORITY_HIGHEST
setting requires elevated privileges. If the user that’s running the program doesn’t have elevated privileges, then it will be silently reduced to PRIORITY_HIGH
.
The following priority constants are provided by Node.js:
PRIORITY_LOW
— The lowest process scheduling priority. This corresponds toIDLE_PRIORITY_CLASS
on Windows and a nice value of 19 on all other platforms.PRIORITY_BELOW_NORMAL
— This corresponds toBELOW_NORMAL_PRIORITY_CLASS
on Windows and a nice value of 10 on all other platforms.PRIORITY_NORMAL
— The default process scheduling priority. This corresponds toNORMAL_PRIORITY_CLASS
on Windows and a nice value of 0 on all other platforms.PRIORITY_ABOVE_NORMAL
— This corresponds toABOVE_NORMAL_PRIORITY_CLASS
on Windows and a nice value of -7 on all other platforms.PRIORITY_HIGH
— This corresponds toHIGH_PRIORITY_CLASS
on Windows and a nice value of -14 on all other platforms.PRIORITY_HIGHEST
— The highest process scheduling priority. This corresponds toREALTIME_PRIORITY_CLASS
on Windows and a nice value of -20 on all other platforms.
For example, we can use it as in the following code:
os.setPriority(0, os.constants.priority.PRIORITY_LOW)
os.tmpdir
The os.tmpdir
function returns a string that specifies the operating system’s default directory for storing temporary files. For example, if we write:
console.log(os.tmpdir());
Then we may get:
'/tmp'
on Linux systems.
os.totalmem
The os.totalmem
function returns the total amount of system memory as a number of bytes in integer form. For example, if we run:
console.log(os.totalmem());
Then we may get something like:
27389460480
os.type
The os.type
function returns a string that identifies the operating system as returned by the uname
command. For example, it may be 'Linux'
on Linux, 'Darwin'
on macOS, and 'Windows_NT'
on Windows. For example, if we run the following:
console.log(os.type());
on a Linux system, then we get ‘Linux’
.
os.uptime
The os.uptime
function returns an integer that has the system uptime in number of seconds. For example, if we run:
console.log(os.uptime());
Then we may get something like:
4928
os.userInfo
The os.userInfo
function returns an object with information about the currently effective user, which is the user identity that the user is assumed.
This may be different from the currently logged in user since a user can run programs as a different user in multiuser operating systems. On POSIX systems, this is typically a subset of the password file.
The returned object includes the username
, uid
, gid
, shell
, and homedir
properties. On Windows, the uid
and gid
are -1, and shell
is null
.
A SystemError
will be thrown if a user has no username
or homedir
. The value of homedir
returned by the os.userInfo
function is provided by the operating system.
This is different from the os.homedir
function which returns a string of the path o the home directory of the user from various environment variables. On Unix and Linux systems, it will use the $HOME
variable if it’s defined.
Otherwise, it will look up the home directory path by the effective UID, which is the user ID of the user identity that you’re currently assuming. For example, if you used sudo
to use the computer as a root user then the effective user ID would be the user ID of the root user.
On Windows, the value of the USERPROFILE
will be used if it’s defined. Otherwise, it will be the path of the profile directory of the current user.
It takes one argument, which is an object that lets us set the options for calling the function. It can have one property, which is the encoding
property, which is a string that indicates the character encoding used to interpret the resulting strings.
The default value is 'utf8'
, but it can also be 'buffer'
, which will set the change the values of the properties of the returned object to Buffer objects. The argument is optional.
For example, if we run the following code:
console.log(os.userInfo({ option: 'utf8' }));
We may get something like the following:
{ uid: 1000,
gid: 1000,
username: 'runner',
homedir: '/home/runner',
shell: '/bin/bash' }
In the output above, the uid
is the user identifier, which identifies the user identity that the currently logged in user has assumed, which may be the currently logged in user if it’s running programs as itself, or it can be a different user like the root user if the Node.js program is currently run as root. This is also called the currently effective user.
The gid
is the group ID. It’s the identifier that associates a user with other users that share something in common. A user can be a member of one or more groups and may have more than one GID.
The username
is the username of the currently effective user. The homedir
is the home directory of the currently effective user. The shell
is the command-line interpreter that’s currently being used by the currently effective user.
The Node.js OS module has many useful utility functions for getting information about the computer system that the OS module’s program is running on.
The modules have many more properties containing useful information like CPUs, endianness, home directory, IP address, hostname, the platform the program is running on, system uptime, information about the currently logged in user and more. It also lets us set the priority of running processes.