In this tutorial, I am going to walk you through the Operating System or OS module in Node.js.
The OS module is a built-in core module and to be able to use it in your projects, you must import it using the require module.
This module provides developers with functions that can be used to retrieve information about the computer and its operating system the program runs on, eventually helps in interacting with the same.
Read about the Node.js Path module here.
Let us begin the tutorial by importing our OS module into our project.
Node.js OS Module – Properties & Methods
As mentioned earlier, the core Node.js OS Module has multiple properties and methods, most of which we will be covering here. So, let’s begin!
Returns the operating system-specific line delimiter sequence.
- \n – POSIX
- \r\n – Windows
Returns the CPU architecture of the operating system for which Node.js binary was compiled. For example, ‘arm’, ‘arm64’, ‘ia32’, ‘mips’, ‘mipsel’, ‘ppc’, ‘ppc64’, ‘s390’, ‘s390x’, ‘x32’, and ‘x64’.
This method contains the commonly-used os-specific constants for error codes, process signals, and more.
The Signal Constants give us all the constants that can be used to handle process signals. These can be accessed through os.constants. However, it is important to note that not all constants are available on all operating systems.
Below are some OS Constants extracted from the official docs. You must access these via os.constants.signals:
|SIGHUP||Sent to indicate when a controlling terminal is closed or a parent process exits.|
|SIGINT||Sent to indicate when a user wishes to interrupt a process (Ctrl+C).|
|SIGQUIT||Sent to indicate when a user wishes to terminate a process and perform a core dump.|
|SIGILL||Sent to a process to notify that it has attempted to perform an illegal, malformed, unknown, or privileged instruction.|
|SIGTRAP||Sent to a process when an exception has occurred.|
|SIGABRT||Sent to a process to request that it abort.|
|SIGIOT||Synonym for SIGABRT|
|SIGBUS||Sent to a process to notify that it has caused a bus error.|
|SIGFPE||Sent to a process to notify that it has performed an illegal arithmetic operation.|
|SIGKILL||Sent to a process to terminate it immediately.|
|SIGUSR1 SIGUSR2||Sent to a process to identify user-defined conditions.|
|SIGSEGV||Sent to a process to notify of a segmentation fault.|
|SIGPIPE||Sent to a process when it has attempted to write to a disconnected pipe.|
|SIGALRM||Sent to a process when a system timer elapses.|
|SIGTERM||Sent to a process to request termination.|
|SIGCHLD||Sent to a process when a child process terminates.|
|SIGSTKFLT||Sent to a process to indicate a stack fault on a coprocessor.|
|SIGCONT||Sent to instruct the operating system to continue a paused process.|
|SIGSTOP||Sent to instruct the operating system to halt a process.|
|SIGTSTP||Sent to a process to request it to stop.|
|SIGBREAK||Sent to indicate when a user wishes to interrupt a process.|
|SIGTTIN||Sent to a process when it reads from the TTY while in the background.|
|SIGTTOU||Sent to a process when it writes to the TTY while in the background.|
|SIGURG||Sent to a process when a socket has urgent data to read.|
|SIGXCPU||Sent to a process when it has exceeded its limit on CPU usage.|
|SIGXFSZ||Sent to a process when it grows a file larger than the maximum allowed.|
|SIGVTALRM||Sent to a process when a virtual timer has elapsed.|
|SIGPROF||Sent to a process when a system timer has elapsed.|
|SIGWINCH||Sent to a process when the controlling terminal has changed its size.|
|SIGIO||Sent to a process when I/O is available.|
|SIGPOLL||Synonym for SIGIO|
|SIGLOST||Sent to a process when a file lock has been lost.|
|SIGPWR||Sent to a process to notify of a power failure.|
|SIGINFO||Synonym for SIGPWR|
|SIGSYS||Sent to a process to notify of a bad argument.|
|SIGUNUSED||Synonym for SIGSYS|
Returns information of the available CPUs on your system. Let’s take an example from the Official Node.js docs:
model: 'Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz',
model: 'Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz',
Checks if Node.js was compiled on Big Endian or Little Endian. Returns BE or LE depending on the result.
Returns the amount of free memory available on the computer, in bytes as an integer.
Returns the current user’s home directory as a string.
On the POSIX operating system, the $HOME environment variable is used if defined. Otherwise, it looks up the users’ home directory using the effective UID.
On the Windows operating system, the USERPROFILE environment variable is used if defined. Otherwise, the path to the current user’s profile directory is used.
Returns a string for the hostname of the operating system.
This method returns an array that contains 1, 5, and 15-minute load averages. The load average refers to a measure of system activity by the OS and is expressed as a fraction.
The output is only logical on Linux or macOS operating systems. On Windows, the output is always [0, 0, 0].
Returns an object that contains the network interfaces assigned to a network address.
Every key on the returned object indicates a network interface. The values associated with those keys are an array of objects that describe an assigned network address.
Returns the platform on which Node.js was compiled. Possible values include:
- android – this is returned if Node.js was compiled on an Android OS.
Returns the release number of the operating system, as a string.
Returns the string that defines a path to the operating system’s default directory of temporary files or the ‘temp’ directory.
Returns the total memory available on the computer, in bytes as an integer.
Returns the name of the operating system. Possible outputs include:
- Linux (Linux)
- Darwin (MacOS)
- Windows_NT (Windows)
Returns time in seconds as to how long the computer has been running since its last boot.
Returns an object which comprises the current user’s uid, username, gid, shell & homedir.
Returns the kernel version of the operating system as a string.
The OS module is a built-in core module and provides developers with functions to retrieve information about the computer and its operating system and help in interaction.