OS#

Stability: 2 - Stable

Source Code:lib/os.js

Thenode:os module provides operating system-related utility methods andproperties. It can be accessed using:

import osfrom'node:os';const os =require('node:os');

os.EOL#

Added in: v0.7.8

The operating system-specific end-of-line marker.

  • \n on POSIX
  • \r\n on Windows

os.availableParallelism()#

Added in: v19.4.0, v18.14.0

Returns an estimate of the default amount of parallelism a program should use.Always returns a value greater than zero.

This function is a small wrapper about libuv'suv_available_parallelism().

os.arch()#

Added in: v0.5.0

Returns the operating system CPU architecture for which the Node.js binary wascompiled. Possible values are'arm','arm64','ia32','loong64','mips','mipsel','ppc64','riscv64','s390x', and'x64'.

The return value is equivalent toprocess.arch.

os.constants#

Added in: v6.3.0

Contains commonly used operating system-specific constants for error codes,process signals, and so on. The specific constants defined are described inOS constants.

os.cpus()#

Added in: v0.3.3

Returns an array of objects containing information about each logical CPU core.The array will be empty if no CPU information is available, such as if the/proc file system is unavailable.

The properties included on each object include:

  • model<string>
  • speed<number> (in MHz)
  • times<Object>
    • user<number> The number of milliseconds the CPU has spent in user mode.
    • nice<number> The number of milliseconds the CPU has spent in nice mode.
    • sys<number> The number of milliseconds the CPU has spent in sys mode.
    • idle<number> The number of milliseconds the CPU has spent in idle mode.
    • irq<number> The number of milliseconds the CPU has spent in irq mode.
[  {model:'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',speed:2926,times: {user:252020,nice:0,sys:30340,idle:1070356870,irq:0,    },  },  {model:'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',speed:2926,times: {user:306960,nice:0,sys:26980,idle:1071569080,irq:0,    },  },  {model:'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',speed:2926,times: {user:248450,nice:0,sys:21750,idle:1070919370,irq:0,    },  },  {model:'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',speed:2926,times: {user:256880,nice:0,sys:19430,idle:1070905480,irq:20,    },  },]

nice values are POSIX-only. On Windows, thenice values of all processorsare always 0.

os.cpus().length should not be used to calculate the amount of parallelismavailable to an application. Useos.availableParallelism() for this purpose.

os.devNull#

Added in: v16.3.0, v14.18.0

The platform-specific file path of the null device.

  • \\.\nul on Windows
  • /dev/null on POSIX

os.endianness()#

Added in: v0.9.4

Returns a string identifying the endianness of the CPU for which the Node.jsbinary was compiled.

Possible values are'BE' for big endian and'LE' for little endian.

os.freemem()#

Added in: v0.3.3

Returns the amount of free system memory in bytes as an integer.

os.getPriority([pid])#

Added in: v10.10.0
  • pid<integer> The process ID to retrieve scheduling priority for.Default:0.
  • Returns:<integer>

Returns the scheduling priority for the process specified bypid. Ifpid isnot provided or is0, the priority of the current process is returned.

os.homedir()#

Added in: v2.3.0

Returns the string path of the current user's home directory.

On POSIX, it uses the$HOME environment variable if defined. Otherwise ituses theeffective UID to look up the user's home directory.

On Windows, it uses theUSERPROFILE environment variable if defined.Otherwise it uses the path to the profile directory of the current user.

os.hostname()#

Added in: v0.3.3

Returns the host name of the operating system as a string.

os.loadavg()#

Added in: v0.3.3

Returns an array containing the 1, 5, and 15 minute load averages.

The load average is a measure of system activity calculated by the operatingsystem and expressed as a fractional number.

The load average is a Unix-specific concept. On Windows, the return value isalways[0, 0, 0].

os.machine()#

Added in: v18.9.0, v16.18.0

Returns the machine type as a string, such asarm,arm64,aarch64,mips,mips64,ppc64,ppc64le,s390x,i386,i686,x86_64.

On POSIX systems, the machine type is determined by callinguname(3). On Windows,RtlGetVersion() is used, and if it is notavailable,GetVersionExW() will be used. Seehttps://en.wikipedia.org/wiki/Uname#Examples for more information.

os.networkInterfaces()#

History
VersionChanges
v18.4.0

Thefamily property now returns a string instead of a number.

v18.0.0

Thefamily property now returns a number instead of a string.

v0.6.0

Added in: v0.6.0

Returns an object containing network interfaces that have been assigned anetwork address.

Each key on the returned object identifies a network interface. The associatedvalue is an array of objects that each describe an assigned network address.

The properties available on the assigned network address object include:

  • address<string> The assigned IPv4 or IPv6 address
  • netmask<string> The IPv4 or IPv6 network mask
  • family<string> EitherIPv4 orIPv6
  • mac<string> The MAC address of the network interface
  • internal<boolean>true if the network interface is a loopback orsimilar interface that is not remotely accessible; otherwisefalse
  • scopeid<number> The numeric IPv6 scope ID (only specified whenfamilyisIPv6)
  • cidr<string> The assigned IPv4 or IPv6 address with the routing prefixin CIDR notation. If thenetmask is invalid, this property is settonull.
{lo: [    {address:'127.0.0.1',netmask:'255.0.0.0',family:'IPv4',mac:'00:00:00:00:00:00',internal:true,cidr:'127.0.0.1/8'    },    {address:'::1',netmask:'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',family:'IPv6',mac:'00:00:00:00:00:00',scopeid:0,internal:true,cidr:'::1/128'    }  ],eth0: [    {address:'192.168.1.108',netmask:'255.255.255.0',family:'IPv4',mac:'01:02:03:0a:0b:0c',internal:false,cidr:'192.168.1.108/24'    },    {address:'fe80::a00:27ff:fe4e:66a1',netmask:'ffff:ffff:ffff:ffff::',family:'IPv6',mac:'01:02:03:0a:0b:0c',scopeid:1,internal:false,cidr:'fe80::a00:27ff:fe4e:66a1/64'    }  ]}

os.platform()#

Added in: v0.5.0

Returns a string identifying the operating system platform for whichthe Node.js binary was compiled. The value is set at compile time.Possible values are'aix','darwin','freebsd','linux','openbsd','sunos', and'win32'.

The return value is equivalent toprocess.platform.

The value'android' may also be returned if Node.js is built on the Androidoperating system.Android support is experimental.

os.release()#

Added in: v0.3.3

Returns the operating system as a string.

On POSIX systems, the operating system release is determined by callinguname(3). On Windows,GetVersionExW() is used. Seehttps://en.wikipedia.org/wiki/Uname#Examples for more information.

os.setPriority([pid, ]priority)#

Added in: v10.10.0
  • pid<integer> The process ID to set scheduling priority for.Default:0.
  • priority<integer> The scheduling priority to assign to the process.

Attempts to set the scheduling priority for the process specified bypid. Ifpid is not provided or is0, the process ID of the current process is used.

Thepriority input must be an integer between-20 (high priority) and19(low priority). Due to differences between Unix priority levels and Windowspriority classes,priority is mapped to one of six priority constants inos.constants.priority. When retrieving a process priority level, this rangemapping may cause the return value to be slightly different on Windows. To avoidconfusion, setpriority to one of the priority constants.

On Windows, setting priority toPRIORITY_HIGHEST requires elevated userprivileges. Otherwise the set priority will be silently reduced toPRIORITY_HIGH.

os.tmpdir()#

History
VersionChanges
v2.0.0

This function is now cross-platform consistent and no longer returns a path with a trailing slash on any platform.

v0.9.9

Added in: v0.9.9

Returns the operating system's default directory for temporary files as astring.

On Windows, the result can be overridden byTEMP andTMP environment variables, andTEMP takes precedence overTMP. If neither is set, it defaults to%SystemRoot%\tempor%windir%\temp.

On non-Windows platforms,TMPDIR,TMP andTEMP environment variables will be checkedto override the result of this method, in the described order. If none of them is set, itdefaults to/tmp.

Some operating system distributions would either configureTMPDIR (non-Windows) orTEMP andTMP (Windows) by default without additional configurations by the systemadministrators. The result ofos.tmpdir() typically reflects the system preferenceunless it's explicitly overridden by the users.

os.totalmem()#

Added in: v0.3.3

Returns the total amount of system memory in bytes as an integer.

os.type()#

Added in: v0.3.3

Returns the operating system name as returned byuname(3). For example, itreturns'Linux' on Linux,'Darwin' on macOS, and'Windows_NT' on Windows.

Seehttps://en.wikipedia.org/wiki/Uname#Examples for additional informationabout the output of runninguname(3) on various operating systems.

os.uptime()#

History
VersionChanges
v10.0.0

The result of this function no longer contains a fraction component on Windows.

v0.3.3

Added in: v0.3.3

Returns the system uptime in number of seconds.

os.userInfo([options])#

Added in: v6.0.0
  • options<Object>
    • encoding<string> Character encoding used to interpret resulting strings.Ifencoding is set to'buffer', theusername,shell, andhomedirvalues will beBuffer instances.Default:'utf8'.
  • Returns:<Object>

Returns information about the currently effective user. On POSIX platforms,this is typically a subset of the password file. The returned object includestheusername,uid,gid,shell, andhomedir. On Windows, theuid andgid fields are-1, andshell isnull.

The value ofhomedir returned byos.userInfo() is provided by the operatingsystem. This differs from the result ofos.homedir(), which queriesenvironment variables for the home directory before falling back to theoperating system response.

Throws aSystemError if a user has nousername orhomedir.

os.version()#

Added in: v13.11.0, v12.17.0

Returns a string identifying the kernel version.

On POSIX systems, the operating system release is determined by callinguname(3). On Windows,RtlGetVersion() is used, and if it is notavailable,GetVersionExW() will be used. Seehttps://en.wikipedia.org/wiki/Uname#Examples for more information.

OS constants#

The following constants are exported byos.constants.

Not all constants will be available on every operating system.

Signal constants#

History
VersionChanges
v5.11.0

Added support forSIGINFO.

The following signal constants are exported byos.constants.signals.

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 forSIGABRT
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.
SIGUSR1SIGUSR2Sent 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 forSIGIO
SIGLOSTSent to a process when a file lock has been lost.
SIGPWRSent to a process to notify of a power failure.
SIGINFOSynonym forSIGPWR
SIGSYSSent to a process to notify of a bad argument.
SIGUNUSEDSynonym forSIGSYS

Error constants#

The following error constants are exported byos.constants.errno.

POSIX error constants#
ConstantDescription
E2BIGIndicates that the list of arguments is longer than expected.
EACCESIndicates that the operation did not have sufficient permissions.
EADDRINUSEIndicates that the network address is already in use.
EADDRNOTAVAILIndicates that the network address is currently unavailable for use.
EAFNOSUPPORTIndicates that the network address family is not supported.
EAGAINIndicates that there is no data available and to try the operation again later.
EALREADYIndicates that the socket already has a pending connection in progress.
EBADFIndicates that a file descriptor is not valid.
EBADMSGIndicates an invalid data message.
EBUSYIndicates that a device or resource is busy.
ECANCELEDIndicates that an operation was canceled.
ECHILDIndicates that there are no child processes.
ECONNABORTEDIndicates that the network connection has been aborted.
ECONNREFUSEDIndicates that the network connection has been refused.
ECONNRESETIndicates that the network connection has been reset.
EDEADLKIndicates that a resource deadlock has been avoided.
EDESTADDRREQIndicates that a destination address is required.
EDOMIndicates that an argument is out of the domain of the function.
EDQUOTIndicates that the disk quota has been exceeded.
EEXISTIndicates that the file already exists.
EFAULTIndicates an invalid pointer address.
EFBIGIndicates that the file is too large.
EHOSTUNREACHIndicates that the host is unreachable.
EIDRMIndicates that the identifier has been removed.
EILSEQIndicates an illegal byte sequence.
EINPROGRESSIndicates that an operation is already in progress.
EINTRIndicates that a function call was interrupted.
EINVALIndicates that an invalid argument was provided.
EIOIndicates an otherwise unspecified I/O error.
EISCONNIndicates that the socket is connected.
EISDIRIndicates that the path is a directory.
ELOOPIndicates too many levels of symbolic links in a path.
EMFILEIndicates that there are too many open files.
EMLINKIndicates that there are too many hard links to a file.
EMSGSIZEIndicates that the provided message is too long.
EMULTIHOPIndicates that a multihop was attempted.
ENAMETOOLONGIndicates that the filename is too long.
ENETDOWNIndicates that the network is down.
ENETRESETIndicates that the connection has been aborted by the network.
ENETUNREACHIndicates that the network is unreachable.
ENFILEIndicates too many open files in the system.
ENOBUFSIndicates that no buffer space is available.
ENODATAIndicates that no message is available on the stream head read queue.
ENODEVIndicates that there is no such device.
ENOENTIndicates that there is no such file or directory.
ENOEXECIndicates an exec format error.
ENOLCKIndicates that there are no locks available.
ENOLINKIndications that a link has been severed.
ENOMEMIndicates that there is not enough space.
ENOMSGIndicates that there is no message of the desired type.
ENOPROTOOPTIndicates that a given protocol is not available.
ENOSPCIndicates that there is no space available on the device.
ENOSRIndicates that there are no stream resources available.
ENOSTRIndicates that a given resource is not a stream.
ENOSYSIndicates that a function has not been implemented.
ENOTCONNIndicates that the socket is not connected.
ENOTDIRIndicates that the path is not a directory.
ENOTEMPTYIndicates that the directory is not empty.
ENOTSOCKIndicates that the given item is not a socket.
ENOTSUPIndicates that a given operation is not supported.
ENOTTYIndicates an inappropriate I/O control operation.
ENXIOIndicates no such device or address.
EOPNOTSUPPIndicates that an operation is not supported on the socket. AlthoughENOTSUP andEOPNOTSUPP have the same value on Linux, according to POSIX.1 these error values should be distinct.)
EOVERFLOWIndicates that a value is too large to be stored in a given data type.
EPERMIndicates that the operation is not permitted.
EPIPEIndicates a broken pipe.
EPROTOIndicates a protocol error.
EPROTONOSUPPORTIndicates that a protocol is not supported.
EPROTOTYPEIndicates the wrong type of protocol for a socket.
ERANGEIndicates that the results are too large.
EROFSIndicates that the file system is read only.
ESPIPEIndicates an invalid seek operation.
ESRCHIndicates that there is no such process.
ESTALEIndicates that the file handle is stale.
ETIMEIndicates an expired timer.
ETIMEDOUTIndicates that the connection timed out.
ETXTBSYIndicates that a text file is busy.
EWOULDBLOCKIndicates that the operation would block.
EXDEVIndicates an improper link.
Windows-specific error constants#

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

ConstantDescription
WSAEINTRIndicates an interrupted function call.
WSAEBADFIndicates an invalid file handle.
WSAEACCESIndicates insufficient permissions to complete the operation.
WSAEFAULTIndicates an invalid pointer address.
WSAEINVALIndicates that an invalid argument was passed.
WSAEMFILEIndicates that there are too many open files.
WSAEWOULDBLOCKIndicates that a resource is temporarily unavailable.
WSAEINPROGRESSIndicates that an operation is currently in progress.
WSAEALREADYIndicates that an operation is already in progress.
WSAENOTSOCKIndicates that the resource is not a socket.
WSAEDESTADDRREQIndicates that a destination address is required.
WSAEMSGSIZEIndicates that the message size is too long.
WSAEPROTOTYPEIndicates the wrong protocol type for the socket.
WSAENOPROTOOPTIndicates a bad protocol option.
WSAEPROTONOSUPPORTIndicates that the protocol is not supported.
WSAESOCKTNOSUPPORTIndicates that the socket type is not supported.
WSAEOPNOTSUPPIndicates that the operation is not supported.
WSAEPFNOSUPPORTIndicates that the protocol family is not supported.
WSAEAFNOSUPPORTIndicates that the address family is not supported.
WSAEADDRINUSEIndicates that the network address is already in use.
WSAEADDRNOTAVAILIndicates that the network address is not available.
WSAENETDOWNIndicates that the network is down.
WSAENETUNREACHIndicates that the network is unreachable.
WSAENETRESETIndicates that the network connection has been reset.
WSAECONNABORTEDIndicates that the connection has been aborted.
WSAECONNRESETIndicates that the connection has been reset by the peer.
WSAENOBUFSIndicates that there is no buffer space available.
WSAEISCONNIndicates that the socket is already connected.
WSAENOTCONNIndicates that the socket is not connected.
WSAESHUTDOWNIndicates that data cannot be sent after the socket has been shutdown.
WSAETOOMANYREFSIndicates that there are too many references.
WSAETIMEDOUTIndicates that the connection has timed out.
WSAECONNREFUSEDIndicates that the connection has been refused.
WSAELOOPIndicates that a name cannot be translated.
WSAENAMETOOLONGIndicates that a name was too long.
WSAEHOSTDOWNIndicates that a network host is down.
WSAEHOSTUNREACHIndicates that there is no route to a network host.
WSAENOTEMPTYIndicates that the directory is not empty.
WSAEPROCLIMIndicates that there are too many processes.
WSAEUSERSIndicates that the user quota has been exceeded.
WSAEDQUOTIndicates that the disk quota has been exceeded.
WSAESTALEIndicates a stale file handle reference.
WSAEREMOTEIndicates that the item is remote.
WSASYSNOTREADYIndicates that the network subsystem is not ready.
WSAVERNOTSUPPORTEDIndicates that thewinsock.dll version is out of range.
WSANOTINITIALISEDIndicates that successful WSAStartup has not yet been performed.
WSAEDISCONIndicates that a graceful shutdown is in progress.
WSAENOMOREIndicates that there are no more results.
WSAECANCELLEDIndicates that an operation has been canceled.
WSAEINVALIDPROCTABLEIndicates that the procedure call table is invalid.
WSAEINVALIDPROVIDERIndicates an invalid service provider.
WSAEPROVIDERFAILEDINITIndicates that the service provider failed to initialized.
WSASYSCALLFAILUREIndicates a system call failure.
WSASERVICE_NOT_FOUNDIndicates that a service was not found.
WSATYPE_NOT_FOUNDIndicates that a class type was not found.
WSA_E_NO_MOREIndicates that there are no more results.
WSA_E_CANCELLEDIndicates that the call was canceled.
WSAEREFUSEDIndicates that a database query was refused.

dlopen constants#

If available on the operating system, the following constantsare exported inos.constants.dlopen. Seedlopen(3) for detailedinformation.

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 ofRTLD_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.

Priority constants#

Added in: v10.10.0

The following process scheduling constants are exported byos.constants.priority.

ConstantDescription
PRIORITY_LOWThe lowest process scheduling priority. This corresponds toIDLE_PRIORITY_CLASS on Windows, and a nice value of19 on all other platforms.
PRIORITY_BELOW_NORMALThe process scheduling priority abovePRIORITY_LOW and belowPRIORITY_NORMAL. This corresponds toBELOW_NORMAL_PRIORITY_CLASS on Windows, and a nice value of10 on all other platforms.
PRIORITY_NORMALThe default process scheduling priority. This corresponds toNORMAL_PRIORITY_CLASS on Windows, and a nice value of0 on all other platforms.
PRIORITY_ABOVE_NORMALThe process scheduling priority abovePRIORITY_NORMAL and belowPRIORITY_HIGH. This corresponds toABOVE_NORMAL_PRIORITY_CLASS on Windows, and a nice value of-7 on all other platforms.
PRIORITY_HIGHThe process scheduling priority abovePRIORITY_ABOVE_NORMAL and belowPRIORITY_HIGHEST. This corresponds toHIGH_PRIORITY_CLASS on Windows, and a nice value of-14 on all other platforms.
PRIORITY_HIGHESTThe highest process scheduling priority. This corresponds toREALTIME_PRIORITY_CLASS on Windows, and a nice value of-20 on all other platforms.

libuv constants#

ConstantDescription
UV_UDP_REUSEADDR