OS module in Node.js

Introduction

In this blog post, we will learn how can you use Node.js core module to get os specific Information. In the early days of JavaScript, it was just a client-side language but after the introduction of Node.js, it has become a server-side language as well. If you are building your web application by using Node.js then getting system-related information is a common programming practice. Node.js built-in OS module provides methods and properties to get operation system-related information. You don’t need any third-party module to get this type of information.

OS module

To use os module first you need to import os module as follows:

const os = require('os')

Let’s see main APIs of os module.

  1. os.arch() : This method returns the operating system CPU architecture for which the Node.js binary was compiled. Possible values are  x64, armarm64.
  2. os.cpus(): This method returns information about each logical CPU core on your system.
  3. os.freemem(): This method returns the number of bytes that represent the free memory in the system.
  4. os.homedir(): This method returns the current user’s home directory.
  5. os.hostname(): This method returns the host name of the operating system as a string.
  6. os.networkInterfaces(): This method returns details of the network interfaces available on the system.
  7. os.platform(): This method returns a string identifying the operating system platform. The value is set at compile time. Possible values are 'aix''darwin''freebsd''linux''openbsd''sunos', and 'win32'
  8. os.release(): This method returns a string which denotes the operating system release number
  9. os.tempdir(): This method returns the operating system’s default directory for temporary files as a string
  10. os.totalmem(): This method returns total avaliable memory on the system in bytes.
  11. os.uptime(): This method returns the system uptime in seconds.
  12. os.userInfo(): This method returns an object that contains the current usernameuidgid, homedir and shell.

Along with APIs os module also provides some properties as follows:

  • os.EOL: gives the line delimiter sequence. It’s \n on Posix, and \r\n on Windows.
  • os.constants: The following constants are exported by os.constants.
ConstantDescription
SIGHUPSent to indicate when a controlling terminal is closed or a parent process exits.
SIGINTSent to indicate when a user wishes to interrupt a process (Ctrl+C).
SIGQUITSent to indicate when a user wishes to terminate a process and perform a core dump.
SIGILLSent to a process to notify that it has attempted to perform an illegal, malformed, unknown, or privileged instruction.
SIGTRAPSent to a process when an exception has occurred.
SIGABRTSent to a process to request that it abort.
SIGIOTSynonym for SIGABRT
SIGBUSSent to a process to notify that it has caused a bus error.
SIGFPESent to a process to notify that it has performed an illegal arithmetic operation.
SIGKILLSent to a process to terminate it immediately.
SIGUSR1 SIGUSR2Sent to a process to identify user-defined conditions.
SIGSEGVSent to a process to notify of a segmentation fault.
SIGPIPESent to a process when it has attempted to write to a disconnected pipe.
SIGALRMSent to a process when a system timer elapses.
SIGTERMSent to a process to request termination.
SIGCHLDSent to a process when a child process terminates.
SIGSTKFLTSent to a process to indicate a stack fault on a coprocessor.
SIGCONTSent to instruct the operating system to continue a paused process.
SIGSTOPSent to instruct the operating system to halt a process.
SIGTSTPSent to a process to request it to stop.
SIGBREAKSent to indicate when a user wishes to interrupt a process.
SIGTTINSent to a process when it reads from the TTY while in the background.
SIGTTOUSent to a process when it writes to the TTY while in the background.
SIGURGSent to a process when a socket has urgent data to read.
SIGXCPUSent to a process when it has exceeded its limit on CPU usage.
SIGXFSZSent to a process when it grows a file larger than the maximum allowed.
SIGVTALRMSent to a process when a virtual timer has elapsed.
SIGPROFSent to a process when a system timer has elapsed.
SIGWINCHSent to a process when the controlling terminal has changed its size.
SIGIOSent to a process when I/O is available.
SIGPOLLSynonym for SIGIO
SIGLOSTSent to a process when a file lock has been lost.
SIGPWRSent to a process to notify of a power failure.
SIGINFOSynonym for SIGPWR
SIGSYSSent to a process to notify of a bad argument.
SIGUNUSEDSynonym for SIGSYS

All constants will not be available on every operating system.

The following error constants are exported by os.constants.errno.

POSIX error constants

ConstantDescription
E2BIGDenotes that the list of arguments is longer than expected.
EACCESDenotes that the operation did not have sufficient permissions.
EADDRINUSEDenotes that the network address is already in use.
EADDRNOTAVAILDenotes that the network address is currently unavailable for use.
EAFNOSUPPORTDenotes that the network address family is not supported.
EAGAINDenotes that there is no data available and to try the operation again later.
EALREADYDenotes that the socket already has a pending connection in progress.
EBADFDenotes that a file descriptor is not valid.
EBADMSGDenotes an invalid data message.
EBUSYDenotes that a device or resource is busy.
ECANCELEDDenotes that an operation was canceled.
ECHILDDenotes that there are no child processes.
ECONNABORTEDDenotes that the network connection has been aborted.
ECONNREFUSEDDenotes that the network connection has been refused.
ECONNRESETDenotes that the network connection has been reset.
EDEADLKDenotes that a resource deadlock has been avoided.
EDESTADDRREQDenotes that a destination address is required.
EDOMDenotes that an argument is out of the domain of the function.
EDQUOTDenotes that the disk quota has been exceeded.
EEXISTDenotes that the file already exists.
EFAULTDenotes an invalid pointer address.
EFBIGDenotes that the file is too large.
EHOSTUNREACHDenotes that the host is unreachable.
EIDRMDenotes that the identifier has been removed.
EILSEQDenotes an illegal byte sequence.
EINPROGRESSDenotes that an operation is already in progress.
EINTRDenotes that a function call was interrupted.
EINVALDenotes that an invalid argument was provided.
EIODenotes an otherwise unspecified I/O error.
EISCONNDenotes that the socket is connected.
EISDIRDenotes that the path is a directory.
ELOOPDenotes too many levels of symbolic links in a path.
EMFILEDenotes that there are too many open files.
EMLINKDenotes that there are too many hard links to a file.
EMSGSIZEDenotes that the provided message is too long.
EMULTIHOPDenotes that a multihop was attempted.
ENAMETOOLONGDenotes that the filename is too long.
ENETDOWNDenotes that the network is down.
ENETRESETDenotes that the connection has been aborted by the network.
ENETUNREACHDenotes that the network is unreachable.
ENFILEDenotes too many open files in the system.
ENOBUFSDenotes that no buffer space is available.
ENODATADenotes that no message is available on the stream head to read queue.
ENODEVDenotes that there is no such device.
ENOENTDenotes that there is no such file or directory.
ENOEXECDenotes an exec format error.
ENOLCKDenotes that there are no locks available.
ENOLINKDenotes that a link has been severed.
ENOMEMDenotes that there is not enough space.
ENOMSGDenotes that there is no message of the desired type.
ENOPROTOOPTDenotes that a given protocol is not available.
ENOSPCDenotes that there is no space available on the device.
ENOSRDenotes that there are no stream resources available.
ENOSTRDenotes that a given resource is not a stream.
ENOSYSDenotes that a function has not been implemented.
ENOTCONNDenotes that the socket is not connected.
ENOTDIRDenotes that the path is not a directory.
ENOTEMPTYDenotes that the directory is not empty.
ENOTSOCKDenotes that the given item is not a socket.
ENOTSUPDenotes that a given operation is not supported.
ENOTTYDenotes an inappropriate I/O control operation.
ENXIODenotes no such device or address.
EOPNOTSUPPDenotes that an operation is not supported on the socket. Although ENOTSUP and EOPNOTSUPP have the same value on Linux, according to POSIX.1 these error values should be distinct.)
EOVERFLOWDenotes that a value is too large to be stored in a given data type.
EPERMDenotes that the operation is not permitted.
EPIPEDenotes a broken pipe.
EPROTODenotes a protocol error.
EPROTONOSUPPORTDenotes that a protocol is not supported.
EPROTOTYPEDenotes the wrong type of protocol for a socket.
ERANGEDenotes that the results are too large.
EROFSDenotes that the file system is read only.
ESPIPEDenotes an invalid seek operation.
ESRCHDenotes that there is no such process.
ESTALEDenotes that the file handle is stale.
ETIMEDenotes an expired timer.
ETIMEDOUTDenotes that the connection timed out.
ETXTBSYDenotes that a text file is busy.
EWOULDBLOCKDenotes that the operation would block.
EXDEVDenotes an improper link.

Windows-specific error constants

The following error codes are specific to the Windows operating system.

ConstantDescription
WSAEINTRDenotes an interrupted function call.
WSAEBADFDenotes an invalid file handle.
WSAEACCESDenotes insufficient permissions to complete the operation.
WSAEFAULTDenotes an invalid pointer address.
WSAEINVALDenotes that an invalid argument was passed.
WSAEMFILEDenotes that there are too many open files.
WSAEWOULDBLOCKDenotes that a resource is temporarily unavailable.
WSAEINPROGRESSDenotes that an operation is currently in progress.
WSAEALREADYDenotes that an operation is already in progress.
WSAENOTSOCKDenotes that the resource is not a socket.
WSAEDESTADDRREQDenotes that a destination address is required.
WSAEMSGSIZEDenotes that the message size is too long.
WSAEPROTOTYPEDenotes the wrong protocol type for the socket.
WSAENOPROTOOPTDenotes a bad protocol option.
WSAEPROTONOSUPPORTDenotes that the protocol is not supported.
WSAESOCKTNOSUPPORTDenotes that the socket type is not supported.
WSAEOPNOTSUPPDenotes that the operation is not supported.
WSAEPFNOSUPPORTDenotes that the protocol family is not supported.
WSAEAFNOSUPPORTDenotes that the address family is not supported.
WSAEADDRINUSEDenotes that the network address is already in use.
WSAEADDRNOTAVAILDenotes that the network address is not available.
WSAENETDOWNDenotes that the network is down.
WSAENETUNREACHDenotes that the network is unreachable.
WSAENETRESETDenotes that the network connection has been reset.
WSAECONNABORTEDDenotes that the connection has been aborted.
WSAECONNRESETDenotes that the connection has been reset by the peer.
WSAENOBUFSDenotes that there is no buffer space available.
WSAEISCONNDenotes that the socket is already connected.
WSAENOTCONNDenotes that the socket is not connected.
WSAESHUTDOWNDenotes that data cannot be sent after the socket has been shut down.
WSAETOOMANYREFSDenotes that there are too many references.
WSAETIMEDOUTDenotes that the connection has timed out.
WSAECONNREFUSEDDenotes that the connection has been refused.
WSAELOOPDenotes that a name cannot be translated.
WSAENAMETOOLONGDenotes that a name was too long.
WSAEHOSTDOWNDenotes that a network host is down.
WSAEHOSTUNREACHDenotes that there is no route to a network host.
WSAENOTEMPTYDenotes that the directory is not empty.
WSAEPROCLIMDenotes that there are too many processes.
WSAEUSERSDenotes that the user quota has been exceeded.
WSAEDQUOTDenotes that the disk quota has been exceeded.
WSAESTALEDenotes a stale file handle reference.
WSAEREMOTEDenotes that the item is remote.
WSASYSNOTREADYDenotes that the network subsystem is not ready.
WSAVERNOTSUPPORTEDIndicates that the winsock.dll version is out of range.
WSANOTINITIALISEDDenotes that successful WSAStartup has not yet been performed.
WSAEDISCONDenotes that a graceful shutdown is in progress.
WSAENOMOREDenotes that there are no more results.
WSAECANCELLEDDenotes that an operation has been canceled.
WSAEINVALIDPROCTABLEDenotes that the procedure call table is invalid.
WSAEINVALIDPROVIDERDenotes an invalid service provider.
WSAEPROVIDERFAILEDINITDenotes that the service provider failed to initialize.
WSASYSCALLFAILUREDenotes a system call failure.
WSASERVICE_NOT_FOUNDDenotes that a service was not found.
WSATYPE_NOT_FOUNDDenotes that a class type was not found.
WSA_E_NO_MOREDenotes that there are no more results.
WSA_E_CANCELLEDDenotes that the call was canceled.
WSAEREFUSEDDenotes that a database query was refused.

dlopen constants

If available on the operating system, the following constants are exported in os.constants.dlopen. See dlopen(3) for detailed information.

ConstantDescription
RTLD_LAZYPerform lazy binding. Node.js sets this flag by default.
RTLD_NOWResolve all undefined symbols in the library before dlopen(3) returns.
RTLD_GLOBALSymbols defined by the library will be made available for symbol resolution of subsequently loaded libraries.
RTLD_LOCALThe converse of RTLD_GLOBAL. This is the default behavior if neither flag is specified.
RTLD_DEEPBINDMake a self-contained library use its own symbols in preference to symbols from previously loaded libraries.

#os module node.js #how to measure cpu usage using to os module node.js #os module nodejs #node.js os module example #os module example in node.js # os module node.js APIs #os module functions node.js #os module #os module javascript #os module explained #os module usage in node.js #os module example nodejs #node.js os module #nodejs os module #calculate CPU usage in node.js # calculate memory usage in node.js #free memory in node.js node.js os platform #node.js os type #node.js os module not available #how to update node.js os x #node.js os support #node.js os module not available ionic #node.js os module function not #node.js os cpu usage #node.js os module #availabe memory nodejs

, , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site