2015-09-30



Table of Contents

process

Exit Codes

Event: 'exit'

Event: 'message'

Event: 'beforeExit'

Event: 'uncaughtException'

Event: 'unhandledRejection'

Event: 'rejectionHandled'

Signal Events

process.stdout

process.stderr

process.stdin

process.argv

process.execPath

process.execArgv

process.abort()

process.chdir(directory)

process.cwd()

process.env

process.exit([code])

process.exitCode

process.getgid()

process.getegid()

process.setgid(id)

process.setegid(id)

process.getuid()

process.geteuid()

process.setuid(id)

process.seteuid(id)

process.getgroups()

process.setgroups(groups)

process.initgroups(user, extra_group)

process.version

process.versions

process.config

process.release

process.kill(pid[, signal])

process.pid

process.title

process.arch

process.platform

process.memoryUsage()

process.nextTick(callback[, arg][, ...])

process.umask([mask])

process.uptime()

process.hrtime()

process.send(message[, sendHandle][, callback])

process.disconnect()

process.connected

process.mainModule

process#
The process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter.

Exit Codes#
Node.js will normally exit with a 0 status code when no more async operations are pending. The following status codes are used in other cases:

1 Uncaught Fatal Exception - There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler.

2 - Unused (reserved by Bash for builtin misuse)

3 Internal JavaScript Parse Error - The JavaScript source code internal in Node.js's bootstrapping process caused a parse error. This is extremely rare, and generally can only happen during development of Node.js itself.

4 Internal JavaScript Evaluation Failure - The JavaScript source code internal in Node.js's bootstrapping process failed to return a function value when evaluated. This is extremely rare, and generally can only happen during development of Node.js itself.

5 Fatal Error - There was a fatal unrecoverable error in V8. Typically a message will be printed to stderr with the prefix FATAL ERROR.

6 Non-function Internal Exception Handler - There was an uncaught exception, but the internal fatal exception handler function was somehow set to a non-function, and could not be called.

7 Internal Exception Handler Run-Time Failure - There was an uncaught exception, and the internal fatal exception handler function itself threw an error while attempting to handle it. This can happen, for example, if a process.on('uncaughtException') or domain.on('error') handler throws an error.

8 - Unused. In previous versions of Node.js, exit code 8 sometimes indicated an uncaught exception.

9 - Invalid Argument - Either an unknown option was specified, or an option requiring a value was provided without a value.

10 Internal JavaScript Run-Time Failure - The JavaScript source code internal in Node.js's bootstrapping process threw an error when the bootstrapping function was called. This is extremely rare, and generally can only happen during development of Node.js itself.

12 Invalid Debug Argument - The --debug and/or --debug-brkoptions were set, but an invalid port number was chosen.

>128 Signal Exits - If Node.js receives a fatal signal such as SIGKILL or SIGHUP, then its exit code will be 128 plus the value of the signal code. This is a standard Unix practice, since exit codes are defined to be 7-bit integers, and signal exits set the high-order bit, and then contain the value of the signal code.

Event: 'exit'#
Emitted when the process is about to exit. There is no way to prevent the exiting of the event loop at this point, and once all exit listeners have finished running the process will exit. Therefore you must only perform synchronous operations in this handler. This is a good hook to perform checks on the module's state (like for unit tests). The callback takes one argument, the code the process is exiting with.
This event is only emitted when node exits explicitly by process.exit() or implicitly by the event loop draining.
Example of listening for exit:

Event: 'message'#

message Object a parsed JSON object or primitive value

sendHandle Handle object a net.Socket or net.Server object, or undefined.

Messages sent by ChildProcess.send() are obtained using the 'message'event on the child's process object.

Event: 'beforeExit'#
This event is emitted when Node.js empties its event loop and has nothing else to schedule. Normally, Node.js exits when there is no work scheduled, but a listener for 'beforeExit' can make asynchronous calls, and cause Node.js to continue.
'beforeExit' is not emitted for conditions causing explicit termination, such as process.exit() or uncaught exceptions, and should not be used as an alternative to the 'exit' event unless the intention is to schedule more work.

Event: 'uncaughtException'#
Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur.
Example of listening for uncaughtException:

Note that uncaughtException is a very crude mechanism for exception handling.
Do not use it as the Node.js equivalent of On Error Resume Next. An unhandled exception means your application - and by extension Node.js itself - is in an undefined state. Blindly resuming means anything could happen.
Think of resuming as pulling the power cord when you are upgrading your system. Nine out of ten times nothing happens - but the 10th time, your system is bust.
uncaughtException should be used to perform synchronous cleanup before shutting down the process. It is not safe to resume normal operation after uncaughtException. If you do use it, restart your application after every unhandled exception!
You have been warned.

Event: 'unhandledRejection'#
Emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop. When programming with promises exceptions are encapsulated as rejected promises. Such promises can be caught and handled using promise.catch(...) and rejections are propagated through a promise chain. This event is useful for detecting and keeping track of promises that were rejected whose rejections were not handled yet. This event is emitted with the following arguments:

reason the object with which the promise was rejected (usually an Errorinstance).

p the promise that was rejected.

Here is an example that logs every unhandled rejection to the console

For example, here is a rejection that will trigger the 'unhandledRejection'event:

Event: 'rejectionHandled'#
Emitted whenever a Promise was rejected and an error handler was attached to it (for example with .catch()) later than after an event loop turn. This event is emitted with the following arguments:

p the promise that was previously emitted in an 'unhandledRejection' event, but which has now gained a rejection handler.

There is no notion of a top level for a promise chain at which rejections can always be handled. Being inherently asynchronous in nature, a promise rejection can be be handled at a future point in time — possibly much later than the event loop turn it takes for the 'unhandledRejection' event to be emitted.
Another way of stating this is that, unlike in synchronous code where there is an ever-growing list of unhandled exceptions, with promises there is a growing-and-shrinking list of unhandled rejections. In synchronous code, the 'uncaughtException' event tells you when the list of unhandled exceptions grows. And in asynchronous code, the 'unhandledRejection' event tells you when the list of unhandled rejections grows, while the 'rejectionHandled' event tells you when the list of unhandled rejections shrinks.
For example using the rejection detection hooks in order to keep a list of all the rejected promises at a given time:

Signal Events#
Emitted when the processes receives a signal. See sigaction(2) for a list of standard POSIX signal names such as SIGINT, SIGHUP, etc.
Example of listening for SIGINT:

An easy way to send the SIGINT signal is with Control-C in most terminal programs.
Note:

SIGUSR1 is reserved by Node.js to start the debugger. It's possible to install a listener but that won't stop the debugger from starting.

SIGTERM and SIGINT have default handlers on non-Windows platforms that resets the terminal mode before exiting with code 128 + signal number. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit).

SIGPIPE is ignored by default. It can have a listener installed.

SIGHUP is generated on Windows when the console window is closed, and on other platforms under various similar conditions, see signal(7). It can have a listener installed, however Node.js will be unconditionally terminated by Windows about 10 seconds later. On non-Windows platforms, the default behavior of SIGHUP is to terminate Node.js, but once a listener has been installed its default behavior will be removed.

SIGTERM is not supported on Windows, it can be listened on.

SIGINT from the terminal is supported on all platforms, and can usually be generated with CTRL+C (though this may be configurable). It is not generated when terminal raw mode is enabled.

SIGBREAK is delivered on Windows when CTRL+BREAK is pressed, on non-Windows platforms it can be listened on, but there is no way to send or generate it.

SIGWINCH is delivered when the console has been resized. On Windows, this will only happen on write to the console when the cursor is being moved, or when a readable tty is used in raw mode.

SIGKILL cannot have a listener installed, it will unconditionally terminate Node.js on all platforms.

SIGSTOP cannot have a listener installed.

Note that Windows does not support sending Signals, but Node.js offers some emulation with process.kill(), and child_process.kill(). Sending signal 0can be used to test for the existence of a process. Sending SIGINT, SIGTERM, and SIGKILL cause the unconditional termination of the target process.

process.stdout#
A Writable Stream to stdout (on fd 1).
For example, a console.log equivalent could look like this:

process.stderr and process.stdout are unlike other streams in Node.js in that they cannot be closed (end() will throw), they never emit the finishevent and that writes are always blocking.
To check if Node.js is being run in a TTY context, read the isTTY property on process.stderr, process.stdout, or process.stdin:

See the tty docs for more information.

process.stderr#
A writable stream to stderr (on fd 2).
process.stderr and process.stdout are unlike other streams in Node.js in that they cannot be closed (end() will throw), they never emit the finishevent and that writes are usually blocking.

They are blocking in the case that they refer to regular files or TTY file descriptors.

In the case they refer to pipes:

They are blocking in Linux/Unix.

They are non-blocking like other streams in Windows.

process.stdin#
A Readable Stream for stdin (on fd 0).
Example of opening standard input and listening for both events:

As a Stream, process.stdin can also be used in "old" mode that is compatible with scripts written for node.js prior to v0.10. For more information see Stream compatibility.
In "old" Streams mode the stdin stream is paused by default, so one must call process.stdin.resume() to read from it. Note also that calling process.stdin.resume() itself would switch stream to "old" mode.
If you are starting a new project you should prefer a more recent "new" Streams mode over "old" one.

process.argv#
An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

This will generate:

process.execPath#
This is the absolute pathname of the executable that started the process.
Example:

process.execArgv#
This is the set of Node.js-specific command line options from the executable that started the process. These options do not show up in process.argv, and do not include the Node.js executable, the name of the script, or any options following the script name. These options are useful in order to spawn child processes with the same execution environment as the parent.
Example:

results in process.execArgv:

and process.argv:

process.abort()#
This causes Node.js to emit an abort. This will cause Node.js to exit and generate a core file.

process.chdir(directory)#
Changes the current working directory of the process or throws an exception if that fails.

process.cwd()#
Returns the current working directory of the process.

process.env#
An object containing the user environment. See environ(7).
An example of this object looks like:

You can write to this object, but changes won't be reflected outside of your process. That means that the following won't work:

But this will:

process.exit([code])#
Ends the process with the specified code. If omitted, exit uses the 'success' code 0.
To exit with a 'failure' code:

The shell that executed Node.js should see the exit code as 1.

process.exitCode#
A number which will be the process exit code, when the process either exits gracefully, or is exited via process.exit() without specifying a code.
Specifying a code to process.exit(code) will override any previous setting of process.exitCode.

process.getgid()#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Gets the group identity of the process. (See getgid(2).) This is the numerical group id, not the group name.

process.getegid()#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Gets the effective group identity of the process. (See getegid(2).) This is the numerical group id, not the group name.

process.setgid(id)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Sets the group identity of the process. (See setgid(2).) This accepts either a numerical ID or a groupname string. If a groupname is specified, this method blocks while resolving it to a numerical ID.

process.setegid(id)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Sets the effective group identity of the process. (See setegid(2).) This accepts either a numerical ID or a groupname string. If a groupname is specified, this method blocks while resolving it to a numerical ID.

process.getuid()#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Gets the user identity of the process. (See getuid(2).) This is the numerical userid, not the username.

process.geteuid()#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Gets the effective user identity of the process. (See geteuid(2).) This is the numerical userid, not the username.

process.setuid(id)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Sets the user identity of the process. (See setuid(2).) This accepts either a numerical ID or a username string. If a username is specified, this method blocks while resolving it to a numerical ID.

process.seteuid(id)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Sets the effective user identity of the process. (See seteuid(2).) This accepts either a numerical ID or a username string. If a username is specified, this method blocks while resolving it to a numerical ID.

process.getgroups()#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Returns an array with the supplementary group IDs. POSIX leaves it unspecified if the effective group ID is included but Node.js ensures it always is.

process.setgroups(groups)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Sets the supplementary group IDs. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
The list can contain group IDs, group names or both.

process.initgroups(user, extra_group)#
Note: this function is only available on POSIX platforms (i.e. not Windows, Android)
Reads /etc/group and initializes the group access list, using all groups of which the user is a member. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
user is a user name or user ID. extra_group is a group name or group ID.
Some care needs to be taken when dropping privileges. Example:

process.version#
A compiled-in property that exposes NODE_VERSION.

process.versions#
A property exposing version strings of Node.js and its dependencies.

Will print something like:

process.config#
An Object containing the JavaScript representation of the configure options that were used to compile the current Node.js executable. This is the same as the "config.gypi" file that was produced when running the ./configure script.
An example of the possible output looks like:

process.release#
An Object containing metadata related to the current release, including URLs for the source tarball and headers-only tarball.
process.release contains the following properties:

name: a string with a value that will always be "node" for Node.js. For legacy io.js releases, this will be "io.js".

sourceUrl: a complete URL pointing to a .tar.gz file containing the source of the current release.

headersUrl: a complete URL pointing to a .tar.gz file containing only the header files for the current release. This file is significantly smaller than the full source file and can be used for compiling add-ons against Node.js.

libUrl: a complete URL pointing to an node.lib file matching the architecture and version of the current release. This file is used for compiling add-ons against Node.js. This property is only present on Windows builds of Node.js and will be missing on all other platforms.

e.g.

In custom builds from non-release versions of the source tree, only the name property may be present. The additional properties should not be relied upon to exist.

process.kill(pid[, signal])#
Send a signal to a process. pid is the process id and signal is the string describing the signal to send. Signal names are strings like 'SIGINT' or 'SIGHUP'. If omitted, the signal will be 'SIGTERM'. See Signal Events and kill(2) for more information.
Will throw an error if target does not exist, and as a special case, a signal of 0 can be used to test for the existence of a process.
Note that even though the name of this function is process.kill, it is really just a signal sender, like the kill system call. The signal sent may do something other than kill the target process.
Example of sending a signal to yourself:

Note: When SIGUSR1 is received by Node.js it starts the d

Show more