Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
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.
const os = require('os')
Useful properties:
os.EOL gives the line delimiter sequence. It’s \n on Linux and macOS, and \r\n on Windows.
Linux and macOS here mean POSIX-style platforms. Node also runs on other systems (e.g. Windows).
os.constants.signals lists all the constants related to handling process signals, like SIGHUP, SIGKILL and so on.
os.constants.errno lists constants for error codes (e.g. EADDRINUSE, EOVERFLOW).
You can read them all on https://nodejs.org/api/os.html#os_signal_constants.
Main methods:
- 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()
Returns the string that identifies the underlying architecture, like arm, x64, arm64.
os.cpus()
Returns information on the CPUs available on your 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()
Returns BE or LE depending on whether Node was compiled with Big Endian or Little Endian.
os.freemem()
Returns the number of bytes of free memory in the system.
os.homedir()
Returns the path to the home directory of the current user.
Example:
'/Users/flavio'
os.hostname()
Returns the hostname.
os.loadavg()
Returns the load average calculated by the operating system.
It only returns a meaningful value on Linux and macOS.
Example:
[ 3.68798828125, 4.00244140625, 11.1181640625 ]
os.networkInterfaces()
Returns the details of the network interfaces available on your 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()
Returns the platform that Node was compiled for:
darwinfreebsdlinuxopenbsdwin32- …more
os.release()
Returns a string that identifies the operating system release number.
os.tmpdir()
Returns the path to the system’s default temp folder.
os.totalmem()
Returns the total number of bytes of memory available in the system.
os.type()
Identifies the operating system:
LinuxDarwinon macOSWindows_NTon 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.
Lessons in this unit:
| 0: | Introduction |
| 1: | The Node events module |
| 2: | The Node fs module |
| 3: | The Node http module |
| 4: | ▶︎ The Node os module |
| 5: | The Node path module |