The process
object is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require()
.
The process.env
property returns an object containing the user environment. An example of this object looks like:
{ TERM: 'xterm-256color', SHELL: '/usr/local/bin/bash', USER: 'maciej', PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin', PWD: '/Users/maciej', EDITOR: 'vim', SHLVL: '1', HOME: '/Users/maciej', LOGNAME: 'maciej', _: '/usr/local/bin/node' }
You can modify this env
object.
process.env.foo = 'bar'; console.log(process.env.foo); // bar
Assigning a property on process.env
will implicitly convert the value to a string. This behavior is deprecated. Future versions of Node.js may throw an error when the value is not a string, number, or boolean.
process.env.test = null; console.log(process.env.test); // => 'null' process.env.test = undefined; console.log(process.env.test); // => 'undefined'
Use delete
to delete a property from process.env
.
process.env.TEST = 1; delete process.env.TEST; console.log(process.env.TEST); // => undefined
On Windows operating systems, environment variables are case-insensitive.
process.env.TEST = 1; console.log(process.env.test); // => 1
The process.argv
property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath
. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.
// Create a file index.js console.log(process.argv); // Then run the following command - node ./index.js "Santanu Bera" hello there 40 true // You will see the following output - [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\Playground\\Node.js\\index.js', 'Santanu Bera', 'hello', 'there', '40', 'true' ]
To exit a process, use the exit function:
process.exit()
The process.exit()
method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit
uses either the 'success' code 0 or the value of process.exitCode
if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.
process.exit(1); // Exit with a failure process.exit(0); // Exit with a success process.exit(129); // This process failed with a different code
Calling process.exit()
will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout
and process.stderr
.
In most situations, it is not actually necessary to call process.exit()
explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode
property can be set to tell the process which exit code to use when the process exits gracefully.
For instance, the following example illustrates a misuse of the process.exit()
method that could lead to data printed to stdout being truncated and lost:
// This is an example of what *not* to do: if (someConditionNotMet()) { printUsageToStdout(); process.exit(1); }
The reason this is problematic is because writes to process.stdout
in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit()
, however, forces the process to exit before those additional writes to stdout
can be performed.
Rather than calling process.exit()
directly, the code should set the process.exitCode
and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:
// How to properly set the exit code while letting // the process exit gracefully. if (someConditionNotMet()) { printUsageToStdout(); process.exitCode = 1; }
If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit()
.
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.
The process.version
property returns the Node.js version string.
console.log(`Version: ${process.version}`);