The Node.js os module

Ranjul Arumadi
Last Updated: May 13, 2022

Introduction

Modules are the blocks of code encapsulated and communicated with an external application based on its related functionalities. 

 

The Node.js os module contains utility methods and properties for retrieving information and interacting with the operating system and computer on which the program is running. 

 

It can be accessed using:

 

const os = require('os');

 

Are you new to Node.js? Check out Everything you need to know about Node.JS.

Properties of Node.js os module

There are a few valuable properties that tell us important information about how to handle files in the Node.js os module:

 

os.EOL

It provides a sequence of line delimiters.

  • \n on POSIX (Portable Operating System Interface)
  • \r\n on Windows

os.constants

It returns an object containing the operating system specified constants. Constants for error codes, process signals, and other operating system-specific issues are included. 

 

Some of the constants are:

  • SIGTRAP - It is sent to a process when an exception occurs.
  • SIGCHLD - It is sent to a process after a child process terminates.

 

You can explore the complete list of OS constants here.

os.devNull

It is a platform-specific file path of the null device.

  • \\.\nul on Windows
  • /dev/null on POSIX

 

Methods of Node.js os module

Let us look at some of the methods that Node.js os module has in stock.

os.getPriority([pid])

The os.getPriority() method accepts a single optional parameter pid. pid specifies the process id whose scheduling priority we need to get. The default value of pid is 0. This method returns the integer value that specifies the scheduling priority of the process we specified using the pid. If the pid is 0, then the scheduling priority of the current process is returned

os.arch()

Returns the string value of the CPU architecture of the operating system for which the Node.js binary was compiled. Some of the possible values are:

'x64', 'arm', 'ia32', 'mips', 'mipsel', 'ppc64', 's390', and 'x32'.

 

The value that is returned is the same as the process.arch.

os.cpus()

It returns an object array with information on each logical CPU core.

 

Each object has the following properties:

  • model <string>
  • speed <number> (in MHz)
  • times <Object>
    • user <number> The number of milliseconds the CPU has spent in the user mode.
    • nice <number> The number of milliseconds the CPU has spent in the nice mode.
    • sys <number> The number of milliseconds the CPU has spent in the sys mode.
    • idle <number> The number of milliseconds the CPU has spent in the idle mode.
    • irq <number> The number of milliseconds the CPU has spent in the irq mode.
    • Nice values are only specific to POSIX. For Windows, nice values for all processors are always 0.

 

Note: Nice values are user-space values that can be used to control the priority of a process.

 

[
  {
    model: 'Intel(R) Core(TM) i5 CPU         860  @ 2.20GHz',
    speed: 2925,
    times: {
      user: 251020,
      nice: 0,
      sys: 30350,
      idle: 1070256870,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5 CPU         860  @ 2.20GHz',
    speed: 2925,
    times: {
      user: 306860,
      nice: 0,
      sys: 26780,
      idle: 1072569080,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5 CPU         860  @ 2.20GHz',
    speed: 2925,
    times: {
      user: 247450,
      nice: 0,
      sys: 21760,
      idle: 1070819370,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5 CPU         860  @ 2.20GHz',
    speed: 2925,
    times: {
      user: 257880,
      nice: 0,
      sys: 19430,
      idle: 1070805480,
      irq: 20
    }
  },
]

os.endianness()

The endianness of the CPU on which the Node.js binary was compiled is returned as a string. Big-endian ('BE') and small endian ('LE') are two possible values. 

Note: Endianness refers to the order or sequence of bytes of a word of digital data in the computer memory.

os.freemem()

Return the number of bytes representing the system's free memory.

os.homedir()

The string path to the current user's home directory is returned.

If the $HOME environment variable is defined on POSIX, it is used. Otherwise, it looks up the user's home directory using the effective UID.

If the USERPROFILE environment variable is set on Windows, it is used. Otherwise, it uses the current user's profile directory's path.

os.hostname()

The host name of the operating system's owner is returned as a string.

os.loadavg()

Returns an array providing the load averages for 1, 5, and 15 minutes.

The load average is a fractional number that the operating system calculates as a measure of system activity.

The load average is a concept unique to Unix. The return value on Windows is always [0, 0, 0].

os.networkInterfaces()

Returns information about the network interfaces on your system.

 

The following properties are available on the assigned network address object:

  • address <string> The assigned IPv6 or IPv4 address
  • netmask <string> The IPv6 or IPv4 network mask
  • family <string> Either IPv6 or IPv4
  • mac <string> The MAC address of the network interface
  • Internal <boolean> If the network interface is a loopback or equivalent non-remotely accessible interface, true; otherwise, false.
  • scopeid <number> The numeric IPv6 scope ID (only specified when the family is IPv6)
  • cidr <string> The assigned IPv6 or IPv4 address in CIDR(Classless Inter-Domain Routing) notation with the routing prefix. If the netmask is invalid, then this property is set to null.

 

{ lo0:
  [ { address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: 'fe:82:00:00:00:00',
      internal: true },
    { address: '::1',
      netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
      family: 'IPv6',
      mac: 'fe:82:00:00:00:00',
      scopeid: 0,
      internal: true },
    { address: 'fe80::1',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: 'fe:82:00:00:00:00',
      scopeid: 1,
      internal: true } ],
  en1:
  [ { address: 'fe82::9b:8282:d7e6:496e',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: '06:00:00:02:0e:00',
      scopeid: 5,
      internal: false },
    { address: '192.168.1.38',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: '06:00:00:02:0e:00',
      internal: false } ],
  utun0:
  [ { address: 'fe80::2513:72bc:f405:61d0',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: 'fe:80:00:20:00:00',
      scopeid: 8,
      internal: false } ] }

os.platform()

The os.platform() returns a string that identifies the operating system platform the system is running on.

Possible values are 

  • Aix
  • Darwin
  • FreeBSD
  • Linux
  • OpenBSD
  • SunOS
  • win32

os.release()

The operating system release number is returned as a string.

os.tmpdir()

As a string, this method returns the operating system's default temporary file directory.

os.totalmem()

As an integer, it returns the total amount of system memory in bytes.

os.type()

Returns the operating system name.

Example:

'Darwin' on macOS, 'Windows_NT' on Windows, etc.

os.uptime()

This function returns the number of seconds it has been running since the machine was last rebooted.

os.userInfo()

The os.userinfo() returns the current username, uid (), gid (), shell, and homedir are all returned as an object.)

This is usually a subset of the password file on POSIX platforms. The username, uid, gid, shell, and homedir are all included in the returned object. The uid and gid values for Windows are -1, and the shell is null.

os.version()

The kernel version is returned as a string.

Uname(3) function is used to determine the operating system release on PosIX platforms. RtlGetVersion() is used on Windows, and GetVersionExW() is used if RtlGetVersion() is not accessible.

   

Let's see the output of some of these properties and methods:

// Including the OS module and creating its object
var JS_os = require('os');

// Returns the total amount of system memory
// Return data type is bytes
console.log("The total memory in system is: " + JS_os.totalmem() + " bytes.");

// Returns the CPU architecture
console.log("The CPU architecture is: " + JS_os.arch());

// Returns the amount of free system memory
// Return data type is bytes
console.log("Free memory available: " + JS_os.freemem() + " bytes.");

// It returns the operating systems default directory for temp files.
console.log('OS default directory for temp files : ' + JS_os.tmpdir ());

// Returns the operating system name
console.log("Operating system name: " + JS_os.type());

// Returns the platform of the OS
console.log('operating system platform is: ' + JS_os.platform());

// Returns the operating systems release.
console.log('OS release: ' + JS_os.release());

// It returns the hostname of the system
console.log("Hostname of system: " + JS_os.hostname());

// Returns the endianness of the system
console.log("Endianness of system is: " + JS_os.endianness());

 

Output

Frequently Asked Questions

 

1. What is the Node.js os module?

The Node.js os module provides several functions for interacting with the operating system. It's also used to provide information about the operating system on the computer.

 

 2. How do we know the hostname of the operating system in Node.js? 

You can use the os.hostname() to get the operating system's hostname in Node.js. 

 

 3. How can we know the amount of free memory in Node.js?

You can use os.freemem() to know the amount of free memory in your system.

Key Takeaways

The Node.js os module contains utility methods and attributes for accessing information and dealing with the operating system and computer that the program is running on. The functions available inside this module can help you extract valuable data about the system like hostname, free memory, and many other things.

 

If you loved reading this article about the Node.js os module, check out What is Node.js? Where, When & How To Use It? and Why every web developer should learn Node.js?.

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think