Node.js Tutorial – OS

Posted by

The tutorial provides the node.js OS module details. The node.js OS module provides the operating system related utilities and methods for the usage in the node.js application.

Node.js OS module Syntax

The node.js OS module can be accessed by adding the below given command:

const os = require('os');

The below given are the available operating system methods and utilities

OS Method NameOS Method Description
os.homedir( )retruns the home directory of the current user
os.arch()provides the CPU architecture of the Operating System
os.hostname( ) provides the hostname of the Operating System
os.cpus( )provides data related to each cpu/core installed: model, speed (in MHz),
and times (an object containing the number of milliseconds the cpu/core
spent in: user, nice, sys, idle, and irq) . Returns array of object.
os.endianness( )provides the endianness of the cpu.
‘BE’ – big endian , ‘LE’ – little endian.
os.freemem( )provides free system memory in bytes
os.loadavg( )provides the  time fraction taken by system activity, calculated by the
operating system and expressed as a fractional number . Returns array of object with 1, 5, and 15 minute load averages
os.networkinterfaces( )provides list of network interfaces
os.platform( )provides operating system platform details of the server
os.release( )provides operating system release
os.userinfo([options])provides subset of the password file entry for the current user
os.uptime( )provides system uptime in seconds
os.totalmem( )provides total system memory in bytes
os.type( )provides the operating system name
os.tmpdir( )provides operating system default directory for temporary files

Node.js os.EOL example

os.EOL – indicates the operating system end of line specifier .

os.EOL Examples:

\n on POSIX
\r\n on Windows

Save the below commands in the file nodejs_os.js and run it

const os=require('os');  
console.log("os.freemem(): \n",os.freemem());  
console.log("os.homedir(): \n",os.homedir());  
console.log("os.hostname(): \n",os.hostname());  
console.log("os.endianness(): \n",os.endianness());  
console.log("os.loadavg(): \n",os.loadavg());  
console.log("os.platform(): \n",os.platform());  
console.log("os.release(): \n",os.release());  
console.log("os.tmpdir(): \n",os.tmpdir());  
console.log("os.totalmem(): \n",os.totalmem());  
console.log("os.type(): \n",os.type());  
console.log("os.uptime(): \n",os.uptime());  
node.js OS method example

Node.js os signal constants

Indicates commonly used operating system-specific constants for error codes, process signals. The below given are the node.js OS Signal constant exported by os.constants.signals

The below given are the node.js OS Signal constant

ConstantDescription
SIGHUPdepicts when a controlling terminal is closed or a parent process exits.
SIGINT depicts when the user interrupts a process – CTRL +C
SIGQUIT depicts when the user terminates a process and perform a core dump.
SIGILL informs process to notify that it has attempted to perform an illegal, malformed, unknown, or privileged instruction.
SIGTRAP informs process when an exception has occurred.
SIGABRTinforms process to request that it abort.
SIGIOTSynonym for SIGABRT
SIGBUS informs process to notify that it has caused a bus error.
SIGFPE informs process to notify that it has performed an illegal arithmetic operation.
SIGKILL informs process to terminate it immediately.
SIGUSR1, SIGUSR2 informs process to identify user-defined conditions.
SIGSEGV informs process to notify of a segmentation fault.
SIGPIPE informs process when it has attempted to write to a disconnected pipe.
SIGALRM informs process when a system timer elapses.
SIGTERM informs process to request termination.
SIGCHLD informs process when a child process terminates.
SIGSTKFIT informs process to indicate a stack fault on a coprocessor.
SIGCONTindicates the operating system to continue a paused process.
SIGSTOP indicates the operating system to halt a process.
SIGTSTPinforms process to request it to stop.
SIGBREAKindicates when the user interrupts a process.
SIGTTIN informs process when it reads from the TTY while in the background.
SIGTTOU informs process when it writes to the TTY while in the background.
SIGURG informs process when a socket has urgent data to read.
SIGXCPU informs process when it has exceeded its limit on CPU usage.
SIGXFSZ informs process when it grows a file larger than the maximum allowed.
SIGVTALRM informs process when a virtual timer has elapsed.
SIGPROF informs process when a system timer has elapsed.
SIGWINCH informs process when the controlling terminal has changed its size.
SIGIO informs process when I/O is available.
SIGPOLLSynonym for SIGIO
SIGLOST informs process when a file lock has been lost.
SIGPWR informs process to notify of a power failure.
SIGINFOSynonym for SIGPWR
SIGSYS informs process to notify of a bad argument.
SIGUNUSEDSynonym for SIGSYS

Node.js OS Priority Constants

The below given process scheduling constants are exported by os.constants.priority

ConstantDescription

PRIORITY_LOW

depicts the lowest  process scheduling priority. Uses IDLE _PRIORITY_CLASS for Windows. Other platforms – Value 10

PRIORITY_BELOW_NORMAL

indicates the process scheduling priority above PRIORITY_LOW and below PRIORITY_NORMAL.  Uses BELOW_NORMAL_PRIORITY_CLASS for Windows. Other platforms – Value 10

PRIORITY_NORMAL

The default process scheduling priority. Uses NORMAL_PRIORITY_CLASS for Windows. Other platforms – Value 0

PRIORITY_ABOVE_NORMAL

indicates the process scheduling priority above PRIORITY_NORMAL and below PRIORITY_HIGH. Uses ABOVE_NORMAL_PRIORITY_CLASS for Windows. Other platforms – Value -7

PRIORITY_HIGH

indicates the process scheduling priority above PRIORITY_ABOVE_NORMAL and below PRIORITY_HIGHEST. Uses HIGH_PRIORITY_CLASS for Windows. Other platforms – Value -14

PRIORITY_HIGHEST

The highest process scheduling priority. Uses RELATIVE_PRIORITY_CLASS for Windows. Other platforms – Value -20

References

node.js OS Reference Link