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.
- os.arch() : This method returns the operating system CPU architecture for which the Node.js binary was compiled. Possible values are
x64
,arm
,arm64
. - os.cpus(): This method returns information about each logical CPU core on your system.
- os.freemem(): This method returns the number of bytes that represent the free memory in the system.
- os.homedir(): This method returns the current user’s home directory.
- os.hostname(): This method returns the host name of the operating system as a string.
- os.networkInterfaces(): This method returns details of the network interfaces available on the system.
- 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'
- os.release(): This method returns a string which denotes the operating system release number
- os.tempdir(): This method returns the operating system’s default directory for temporary files as a string
- os.totalmem(): This method returns total avaliable memory on the system in bytes.
- os.uptime(): This method returns the system uptime in seconds.
- os.userInfo(): This method returns an object that contains the current
username
,uid
,gid
,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
.
Constant | Description |
---|---|
SIGHUP | Sent to indicate when a controlling terminal is closed or a parent process exits. |
SIGINT | Sent to indicate when a user wishes to interrupt a process (Ctrl+C). |
SIGQUIT | Sent to indicate when a user wishes to terminate a process and perform a core dump. |
SIGILL | Sent to a process to notify that it has attempted to perform an illegal, malformed, unknown, or privileged instruction. |
SIGTRAP | Sent to a process when an exception has occurred. |
SIGABRT | Sent to a process to request that it abort. |
SIGIOT | Synonym for SIGABRT |
SIGBUS | Sent to a process to notify that it has caused a bus error. |
SIGFPE | Sent to a process to notify that it has performed an illegal arithmetic operation. |
SIGKILL | Sent to a process to terminate it immediately. |
SIGUSR1 SIGUSR2 | Sent to a process to identify user-defined conditions. |
SIGSEGV | Sent to a process to notify of a segmentation fault. |
SIGPIPE | Sent to a process when it has attempted to write to a disconnected pipe. |
SIGALRM | Sent to a process when a system timer elapses. |
SIGTERM | Sent to a process to request termination. |
SIGCHLD | Sent to a process when a child process terminates. |
SIGSTKFLT | Sent to a process to indicate a stack fault on a coprocessor. |
SIGCONT | Sent to instruct the operating system to continue a paused process. |
SIGSTOP | Sent to instruct the operating system to halt a process. |
SIGTSTP | Sent to a process to request it to stop. |
SIGBREAK | Sent to indicate when a user wishes to interrupt a process. |
SIGTTIN | Sent to a process when it reads from the TTY while in the background. |
SIGTTOU | Sent to a process when it writes to the TTY while in the background. |
SIGURG | Sent to a process when a socket has urgent data to read. |
SIGXCPU | Sent to a process when it has exceeded its limit on CPU usage. |
SIGXFSZ | Sent to a process when it grows a file larger than the maximum allowed. |
SIGVTALRM | Sent to a process when a virtual timer has elapsed. |
SIGPROF | Sent to a process when a system timer has elapsed. |
SIGWINCH | Sent to a process when the controlling terminal has changed its size. |
SIGIO | Sent to a process when I/O is available. |
SIGPOLL | Synonym for SIGIO |
SIGLOST | Sent to a process when a file lock has been lost. |
SIGPWR | Sent to a process to notify of a power failure. |
SIGINFO | Synonym for SIGPWR |
SIGSYS | Sent to a process to notify of a bad argument. |
SIGUNUSED | Synonym 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
Constant | Description |
---|---|
E2BIG | Denotes that the list of arguments is longer than expected. |
EACCES | Denotes that the operation did not have sufficient permissions. |
EADDRINUSE | Denotes that the network address is already in use. |
EADDRNOTAVAIL | Denotes that the network address is currently unavailable for use. |
EAFNOSUPPORT | Denotes that the network address family is not supported. |
EAGAIN | Denotes that there is no data available and to try the operation again later. |
EALREADY | Denotes that the socket already has a pending connection in progress. |
EBADF | Denotes that a file descriptor is not valid. |
EBADMSG | Denotes an invalid data message. |
EBUSY | Denotes that a device or resource is busy. |
ECANCELED | Denotes that an operation was canceled. |
ECHILD | Denotes that there are no child processes. |
ECONNABORTED | Denotes that the network connection has been aborted. |
ECONNREFUSED | Denotes that the network connection has been refused. |
ECONNRESET | Denotes that the network connection has been reset. |
EDEADLK | Denotes that a resource deadlock has been avoided. |
EDESTADDRREQ | Denotes that a destination address is required. |
EDOM | Denotes that an argument is out of the domain of the function. |
EDQUOT | Denotes that the disk quota has been exceeded. |
EEXIST | Denotes that the file already exists. |
EFAULT | Denotes an invalid pointer address. |
EFBIG | Denotes that the file is too large. |
EHOSTUNREACH | Denotes that the host is unreachable. |
EIDRM | Denotes that the identifier has been removed. |
EILSEQ | Denotes an illegal byte sequence. |
EINPROGRESS | Denotes that an operation is already in progress. |
EINTR | Denotes that a function call was interrupted. |
EINVAL | Denotes that an invalid argument was provided. |
EIO | Denotes an otherwise unspecified I/O error. |
EISCONN | Denotes that the socket is connected. |
EISDIR | Denotes that the path is a directory. |
ELOOP | Denotes too many levels of symbolic links in a path. |
EMFILE | Denotes that there are too many open files. |
EMLINK | Denotes that there are too many hard links to a file. |
EMSGSIZE | Denotes that the provided message is too long. |
EMULTIHOP | Denotes that a multihop was attempted. |
ENAMETOOLONG | Denotes that the filename is too long. |
ENETDOWN | Denotes that the network is down. |
ENETRESET | Denotes that the connection has been aborted by the network. |
ENETUNREACH | Denotes that the network is unreachable. |
ENFILE | Denotes too many open files in the system. |
ENOBUFS | Denotes that no buffer space is available. |
ENODATA | Denotes that no message is available on the stream head to read queue. |
ENODEV | Denotes that there is no such device. |
ENOENT | Denotes that there is no such file or directory. |
ENOEXEC | Denotes an exec format error. |
ENOLCK | Denotes that there are no locks available. |
ENOLINK | Denotes that a link has been severed. |
ENOMEM | Denotes that there is not enough space. |
ENOMSG | Denotes that there is no message of the desired type. |
ENOPROTOOPT | Denotes that a given protocol is not available. |
ENOSPC | Denotes that there is no space available on the device. |
ENOSR | Denotes that there are no stream resources available. |
ENOSTR | Denotes that a given resource is not a stream. |
ENOSYS | Denotes that a function has not been implemented. |
ENOTCONN | Denotes that the socket is not connected. |
ENOTDIR | Denotes that the path is not a directory. |
ENOTEMPTY | Denotes that the directory is not empty. |
ENOTSOCK | Denotes that the given item is not a socket. |
ENOTSUP | Denotes that a given operation is not supported. |
ENOTTY | Denotes an inappropriate I/O control operation. |
ENXIO | Denotes no such device or address. |
EOPNOTSUPP | Denotes 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.) |
EOVERFLOW | Denotes that a value is too large to be stored in a given data type. |
EPERM | Denotes that the operation is not permitted. |
EPIPE | Denotes a broken pipe. |
EPROTO | Denotes a protocol error. |
EPROTONOSUPPORT | Denotes that a protocol is not supported. |
EPROTOTYPE | Denotes the wrong type of protocol for a socket. |
ERANGE | Denotes that the results are too large. |
EROFS | Denotes that the file system is read only. |
ESPIPE | Denotes an invalid seek operation. |
ESRCH | Denotes that there is no such process. |
ESTALE | Denotes that the file handle is stale. |
ETIME | Denotes an expired timer. |
ETIMEDOUT | Denotes that the connection timed out. |
ETXTBSY | Denotes that a text file is busy. |
EWOULDBLOCK | Denotes that the operation would block. |
EXDEV | Denotes an improper link. |
Windows-specific error constants
The following error codes are specific to the Windows operating system.
Constant | Description |
---|---|
WSAEINTR | Denotes an interrupted function call. |
WSAEBADF | Denotes an invalid file handle. |
WSAEACCES | Denotes insufficient permissions to complete the operation. |
WSAEFAULT | Denotes an invalid pointer address. |
WSAEINVAL | Denotes that an invalid argument was passed. |
WSAEMFILE | Denotes that there are too many open files. |
WSAEWOULDBLOCK | Denotes that a resource is temporarily unavailable. |
WSAEINPROGRESS | Denotes that an operation is currently in progress. |
WSAEALREADY | Denotes that an operation is already in progress. |
WSAENOTSOCK | Denotes that the resource is not a socket. |
WSAEDESTADDRREQ | Denotes that a destination address is required. |
WSAEMSGSIZE | Denotes that the message size is too long. |
WSAEPROTOTYPE | Denotes the wrong protocol type for the socket. |
WSAENOPROTOOPT | Denotes a bad protocol option. |
WSAEPROTONOSUPPORT | Denotes that the protocol is not supported. |
WSAESOCKTNOSUPPORT | Denotes that the socket type is not supported. |
WSAEOPNOTSUPP | Denotes that the operation is not supported. |
WSAEPFNOSUPPORT | Denotes that the protocol family is not supported. |
WSAEAFNOSUPPORT | Denotes that the address family is not supported. |
WSAEADDRINUSE | Denotes that the network address is already in use. |
WSAEADDRNOTAVAIL | Denotes that the network address is not available. |
WSAENETDOWN | Denotes that the network is down. |
WSAENETUNREACH | Denotes that the network is unreachable. |
WSAENETRESET | Denotes that the network connection has been reset. |
WSAECONNABORTED | Denotes that the connection has been aborted. |
WSAECONNRESET | Denotes that the connection has been reset by the peer. |
WSAENOBUFS | Denotes that there is no buffer space available. |
WSAEISCONN | Denotes that the socket is already connected. |
WSAENOTCONN | Denotes that the socket is not connected. |
WSAESHUTDOWN | Denotes that data cannot be sent after the socket has been shut down. |
WSAETOOMANYREFS | Denotes that there are too many references. |
WSAETIMEDOUT | Denotes that the connection has timed out. |
WSAECONNREFUSED | Denotes that the connection has been refused. |
WSAELOOP | Denotes that a name cannot be translated. |
WSAENAMETOOLONG | Denotes that a name was too long. |
WSAEHOSTDOWN | Denotes that a network host is down. |
WSAEHOSTUNREACH | Denotes that there is no route to a network host. |
WSAENOTEMPTY | Denotes that the directory is not empty. |
WSAEPROCLIM | Denotes that there are too many processes. |
WSAEUSERS | Denotes that the user quota has been exceeded. |
WSAEDQUOT | Denotes that the disk quota has been exceeded. |
WSAESTALE | Denotes a stale file handle reference. |
WSAEREMOTE | Denotes that the item is remote. |
WSASYSNOTREADY | Denotes that the network subsystem is not ready. |
WSAVERNOTSUPPORTED | Indicates that the winsock.dll version is out of range. |
WSANOTINITIALISED | Denotes that successful WSAStartup has not yet been performed. |
WSAEDISCON | Denotes that a graceful shutdown is in progress. |
WSAENOMORE | Denotes that there are no more results. |
WSAECANCELLED | Denotes that an operation has been canceled. |
WSAEINVALIDPROCTABLE | Denotes that the procedure call table is invalid. |
WSAEINVALIDPROVIDER | Denotes an invalid service provider. |
WSAEPROVIDERFAILEDINIT | Denotes that the service provider failed to initialize. |
WSASYSCALLFAILURE | Denotes a system call failure. |
WSASERVICE_NOT_FOUND | Denotes that a service was not found. |
WSATYPE_NOT_FOUND | Denotes that a class type was not found. |
WSA_E_NO_MORE | Denotes that there are no more results. |
WSA_E_CANCELLED | Denotes that the call was canceled. |
WSAEREFUSED | Denotes 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.
Constant | Description |
---|---|
RTLD_LAZY | Perform lazy binding. Node.js sets this flag by default. |
RTLD_NOW | Resolve all undefined symbols in the library before dlopen(3) returns. |
RTLD_GLOBAL | Symbols defined by the library will be made available for symbol resolution of subsequently loaded libraries. |
RTLD_LOCAL | The converse of RTLD_GLOBAL . This is the default behavior if neither flag is specified. |
RTLD_DEEPBIND | Make 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
Leave a Comment