The Node os module
The os module of Node.js provides useful functions to interact with the underlying system.
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.
1 | const os = require('os') |
There are a few useful properties that tell us some key things related to handling files:
os.EOL
gives the line delimiter sequence. It’s \n
on Linux and macOS, and \r\n
on Windows.
When I say Linux and macOS I mean POSIX platforms. For simplicity, I exclude other less popular operating systems Node can run on.
os.constants.signals
tells us all the constants related to handling process signals, like SIGHUP, SIGKILL, and so on.
os.constants.errno
sets the constants for error reporting, like EADDRINUSE, EOVERFLOW, and more.
You can read them all on link.
Let’s now see the main methods that os
provides:
os.arch()
os.cpus()
os.endianness()
os.freemem()
os.homedir()
os.hostname()
os.loadavg()
os.networkInterfaces()
os.platform()
os.release()
os.tmpdir()
os.totalmem()
os.type()
os.uptime()
os.userInfo()
os.arch()
Return the string that identifies the underlying architecture, like arm
, x64
, arm64
.
os.cpus()
Return information on the CPUs available on your system.
Example:
1 | [ { model: 'Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz', |
os.endianness()
Return BE
or LE
depending on whether Node was compiled with Big Endian or Little Endian.
os.freemem()
Return the number of bytes that represent the free memory in the system.
os.homedir()
Return the path to the home directory of the current user.
Example:
1 | '/Users/flavio' |
os.hostname()
Return the hostname.
os.loadavg()
Return the calculation made by the operating system on the load average.
It only returns a meaningful value on Linux and macOS.
Example:
1 | [ 3.68798828125, 4.00244140625, 11.1181640625 ] |
os.networkInterfaces()
Returns the details of the network interfaces available on your system.
Example:
1 | { lo0: |
os.platform()
Return the platform that Node was compiled for:
darwin
freebsd
linux
openbsd
win32
- …more
os.release()
Returns a string that identifies the operating system release number.
os.tmpdir()
Returns the path to the assigned temp folder.
os.totalmem()
Returns the number of bytes that represent the total memory available in the system.
os.type()
Identifies the operating system:
Linux
Darwin
on macOSWindows_NT
on Windows
os.uptime()
Returns the number of seconds the computer has been running since it was last rebooted.
os.userInfo()
Returns information about the current user