OS API in Node.Js

Learn via video courses

Overview

When we build our application in any tech stack, we sometimes need to interact with the operating system. And every tech stack provides us with certain ways to interact with the operating system. Node.js has an OS API module to help programmers interact with Operating System through their program.

Introduction

We all know about Operating Systems (OS) but anyway let's look briefly into it. An operating system is system software that provides common services for computer programs, and it also manages the computer's hardware and software resources. Some common operating systems are windows, macOS, Linux, etc.

operating system example

Node.js also provides the facility to interact with OS (operating system) through the OS module.

OS Module in Node JS

OS module in Node.js establishes interaction between the operating system and the application. The OS module consists of certain properties and methods which help in performing system-related activities. The OS module provides several information about the Operating System to Node.js like hostname, free (unused) memory, total memory, operating system name, user information, etc.

OS module can be included in a program by the required function:

Syntax:

os.EOL Property

os.EOL property returns the operating system-specific end-of-line marker. The end-of-line marker is specified by the operating system where it's running, like for Windows end-of-line marker is "\r\n" whereas for POSIX OS it's "\n".

Program to demonstrate the OS.EOL property of OS module:

Output:

Explanation: Since I have run this program on the Windows Operating system, the output is "\r\n". In the print statement of the above program, we have to stringify os.EOL to JSON to print the characters of os.EOL else it would have simply printed a blank line (end of line).

Methods for the OS Module

Similar to other modules of Node.js, OS module also provides a bunch of methods for our use. Some of them are explained below:

  • type(): os.type() method returns the operating system name according to uname (short for UNIX name). For example, it returns 'Linux' on Linux, 'Darwin' on macOS, and 'Windows_NT' on Windows.

Example:

Output:

  • arch(): os.arch() method returns the operating system CPU architecture of our machine on which the Node.js was compiled. The possible return values can be 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', and 'x64'.

Example:

Output:

  • loadavg(): os.loadavg() method returns an array containing the 1, 5, and 15 minute load averages. The load average is the average system load on the system for a defined period of time. Load average is critical in understanding how our systems are performing.

Example:

Output:

Explanation: The load average is a Unix-specific concept. So on Windows, the return value is always [0, 0, 0].

  • hostname(): os.hostname() method returns the hostname of the operating system.

Example:

Output:

  • totalmem(): os.totalmem() method returns the total system memory in bytes as an integer.

Example:

Output:

  • freemem(): os.freemem() method returns the amount of free (available) system memory in bytes as an integer.

Example:

Output:

  • constants: os.constants contains operating system-specific constants for process signals, error codes, etc.

Example:

Output:

  • release(): os.release() returns the operating system release according to uname (short for unix name).

Example:

Output:

  • uptime(): os.uptime() returns the system uptime in number of seconds i.e., how long the processor has been functioning since it was started.

Example:

Output:

  • cpus(): os.cpus() returns an array of objects containing information about each logical CPU core. Each object includes properties like model, speed, user, sys, idle, etc.

Example:

Output:

  • networkInterfaces(): os.networkInterfaces() returns an object containing network interface information that has been assigned to a network address. Each network address object includes properties like: address, netmask, family (i.e., IPv4 or IPv6), mac, etc.

Example:

Output:

  • platform(): os.platform() returns the operating system platform for which the Node.js binary was compiled. The return value can be any of these depending on operating system: 'aix', 'darwin', 'freebsd','linux', 'openbsd', 'sunos', and 'win32'. The value is set at the compile time.

Example:

Output:

Conclusion

  • OS module in Node.js provides information about the operating system and also helps users interact with the operating system.
  • The os module provides API for retrieving information about hardware-related components like CPU, memory, directories, IP address, etc.
  • os.EOL is a constant which returns the end-of-line marker as specified by the operating system on which the Node.js program is running.
  • OS module has several methods to get information about the operating system like:
    • type(): gives operating system name.
    • arch(): gives operating system CPU architecture.
    • loadavg(): gives the measure of system activity calculated by the operating system.
    • hostname(): gives the hostname of the operating system.
    • totalmem(): gives the total system memory in bytes.
    • freemem(): give the free (unused) system memory in bytes.