2 min read

OS Module in Node JS

The os module provides operating system-related utility methods and properties. It can be accessed using: const os = require('os');
OS Module in Node JS

This module provides many functions that you can use to retrieve information from the underlying operating system and the computer the program runs on, and interact with it.

List of Functions

os.arch()

Returns the operating system CPU architecture for which the Node.js binary was compiled. Possible values are 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'.

os.constants

Contains commonly used operating system-specific constants for error codes, process signals, and so on. The specific constants defined are described in OS constants.

os.cpus()

Returns an array of objects containing information about each logical CPU core.

The properties included on each object include:

  • model <string>
  • speed <number> (in MHz)
  • times <Object>
  • user <number> The number of milliseconds the CPU has spent in user mode.
  • nice <number> The number of milliseconds the CPU has spent in nice mode.
  • sys <number> The number of milliseconds the CPU has spent in sys mode.
  • idle <number> The number of milliseconds the CPU has spent in idle mode.
  • irq <number> The number of milliseconds the CPU has spent in irq mode.

os.freemem()

Returns the amount of free system memory in bytes as an integer.

os.getPriority([pid])

Returns the scheduling priority for the process specified by pid. If pid is not provided or is 0, the priority of the current process is returned.

os.homedir()

Returns the string path of the current user's home directory.

On POSIX, it uses the $HOME environment variable if defined. Otherwise it uses the effective UID to look up the user's home directory.

On Windows, it uses the USERPROFILE environment variable if defined. Otherwise it uses the path to the profile directory of the current user.

os.hostname()

Returns the host name of the operating system as a string.

os.networkInterfaces()

Returns an object containing network interfaces that have been assigned a network address.

Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address.

The properties available on the assigned network address object include:

  • address <string> The assigned IPv4 or IPv6 address
  • netmask <string> The IPv4 or IPv6 network mask
  • family <string> Either IPv4 or IPv6
  • mac <string> The MAC address of the network interface
  • internal <boolean> true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false
  • scopeid <number> The numeric IPv6 scope ID (only specified when family is IPv6)
  • cidr <string> The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. If the netmask is invalid, this property is set to null.
OS | Node.js v16.3.0 Documentation
https://nodejs.org/api/os.html

#JavaScript #Node JS #OS #Probyto #ProbytoAI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto