What is the OS Module in Node.js?

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.

const os = require(‘os’);

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!

os.EOL

Returns the operating system-specific line delimiter sequence.

  • \n – POSIX
  • \r\n – Windows

os.arch()

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’.

os.constants()

This method contains the commonly-used os-specific constants for error codes, process signals, and more.

Signal Constants

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:

ConstantDescription
SIGHUPSent to indicate when a controlling terminal is closed or a parent process exits.
SIGINTSent to indicate when a user wishes to interrupt a process (Ctrl+C).
SIGQUITSent to indicate when a user wishes to terminate a process and perform a core dump.
SIGILLSent to a process to notify that it has attempted to perform an illegal, malformed, unknown, or privileged instruction.
SIGTRAPSent to a process when an exception has occurred.
SIGABRTSent to a process to request that it abort.
SIGIOTSynonym for SIGABRT
SIGBUSSent to a process to notify that it has caused a bus error.
SIGFPESent to a process to notify that it has performed an illegal arithmetic operation.
SIGKILLSent to a process to terminate it immediately.
SIGUSR1 SIGUSR2Sent to a process to identify user-defined conditions.
SIGSEGVSent to a process to notify of a segmentation fault.
SIGPIPESent to a process when it has attempted to write to a disconnected pipe.
SIGALRMSent to a process when a system timer elapses.
SIGTERMSent to a process to request termination.
SIGCHLDSent to a process when a child process terminates.
SIGSTKFLTSent to a process to indicate a stack fault on a coprocessor.
SIGCONTSent to instruct the operating system to continue a paused process.
SIGSTOPSent to instruct the operating system to halt a process.
SIGTSTPSent to a process to request it to stop.
SIGBREAKSent to indicate when a user wishes to interrupt a process.
SIGTTINSent to a process when it reads from the TTY while in the background.
SIGTTOUSent to a process when it writes to the TTY while in the background.
SIGURGSent to a process when a socket has urgent data to read.
SIGXCPUSent to a process when it has exceeded its limit on CPU usage.
SIGXFSZSent to a process when it grows a file larger than the maximum allowed.
SIGVTALRMSent to a process when a virtual timer has elapsed.
SIGPROFSent to a process when a system timer has elapsed.
SIGWINCHSent to a process when the controlling terminal has changed its size.
SIGIOSent to a process when I/O is available.
SIGPOLLSynonym for SIGIO
SIGLOSTSent to a process when a file lock has been lost.
SIGPWRSent to a process to notify of a power failure.
SIGINFOSynonym for SIGPWR
SIGSYSSent to a process to notify of a bad argument.
SIGUNUSEDSynonym for SIGSYS

os.cpus()

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',
    speed: 2400,
    times: {
      user: 281685380,
      nice: 0,
      sys: 187986530,
      idle: 685833750,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
    speed: 2400,
    times: {
      user: 282348700,
      nice: 0,
      sys: 161800480,
      idle: 703509470,
      irq: 0
    }
  }
]

os.endianness()

Checks if Node.js was compiled on Big Endian or Little Endian. Returns BE or LE depending on the result.

os.freemem()

Returns the amount of free memory available on the computer, in bytes as an integer.

os.homedir()

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.

For example:

‘Users/Cassandra’

os.hostname()

Returns a string for the hostname of the operating system.

os.loadavg()

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].

os.networkInterfaces()

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.

os.platform()

Returns the platform on which Node.js was compiled. Possible values include:

  • freebsd
  • darwin
  • linux
  • win32
  • openbsd
  • aix
  • sunos
  • android – this is returned if Node.js was compiled on an Android OS.

os.release()

Returns the release number of the operating system, as a string.

os.tmpdir()

Returns the string that defines a path to the operating system’s default directory of temporary files or the ‘temp’ directory.

os.totalmem()

Returns the total memory available on the computer, in bytes as an integer.

os.type()

Returns the name of the operating system. Possible outputs include:

  • Linux (Linux)
  • Darwin (MacOS)
  • Windows_NT (Windows)

os.uptime()

Returns time in seconds as to how long the computer has been running since its last boot.

os.userInfo([options])

Returns an object which comprises the current user’s uid, username, gid, shell & homedir.

os.version()

Returns the kernel version of the operating system as a string.

Conclusion

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.

Noteworthy References

OS Module – Official Node.js Docs

The Node.js OS Module – Official Node.js Docs

Aneesha S
Aneesha S
Articles: 171