Node os module

The os module of Node.js provides useful functions to interact with the underlying system

This module provides many functions, which can be used to retrieve information from and interact with the underlying operating system and the computer where the program is running.

const os = require('os')

There are some useful attributes that can tell us some key things related to processing files:

os.EOLGive the sequence of row delimiters. it is\nOn Linux and macOS, and\r\nOn Windows.

When I say Linux and macOS, I mean the POSIX platform. For simplicity, I excluded other less popular operating systems that Node can run on.

os.constants.signalsTell us all the constants related to the process signal, such as SIGHUP, SIGKILL, etc.

os.constants.errnoSet constants for error reporting, such as EADDRINUSE, EOVERFLOW, etc.

You can read everythinghttps://nodejs.org/api/os.html#os_signal_constants.

Now let's look at the main methodosprovide:

os.arch()

Returns a string identifying the infrastructure, for examplearm,x64,arm64.

os.cpus()

Returns information about the CPUs available on the system.

example:

[ { 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()

returnBEorLEDepends on whether to compile with NodeBig-endian or little-endian.

os.freemem()

Returns the number of bytes representing the available memory in the system.

os.homedir()

Return the path to the current user's home directory.

example:

'/Users/flavio'

os.hostname()

Returns the host name.

os.loadavg()

Returns the calculation of the average load performed by the operating system.

It only returns meaningful values on Linux and macOS.

example:

[ 3.68798828125, 4.00244140625, 11.1181640625 ]

os.networkInterfaces()

Returns detailed information about the network interfaces available on the system.

example:

{ 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()

Return to the platform compiled for Node:

  • darwin
  • freebsd
  • linux
  • openbsd
  • win32
  • …more

os.release()

Returns a string identifying the release number of the operating system

os.tmpdir()

Returns the path of the specified temporary folder.

os.totalmem()

Returns the number of bytes representing the total memory available in the system.

os.type()

Identify the operating system:

  • Linux
  • DarwinOn macOS
  • Windows_NTOn windows

os.uptime()

Returns the number of seconds the computer has been running since the computer was last restarted.

os.userInfo()

Return information about the current user

Download mine for freeNode.js manual


More node tutorials: