process.exit([code])


  • code <integer> 结束状态码。默认为0

process.exit()方法以结束状态码code指示Node.js同步终止进程。 如果code未提供,此exit方法要么使用'success' 状态码 0,要么使用process.exitCode属性值,前提是此属性已被设置。 Node.js在所有'exit'事件监听器都被调用了以后,才会终止进程。

使用一个'failure'状态码结束的例子:

process.exit(1);

执行Node.js的shell应该会得到结束状态码1

调用 process.exit() 会强制进程尽快结束,即使仍然有很多处于等待中的异步操作没有全部执行完成, 包括输出到 process.stdoutprocess.stderr 的 I/O 操作。

在大多数情况下,显式调用process.exit()是没有必要的。如果在事件轮询队列中没有处于等待中的工作,Node.js进程会自行结束。 当进程正常结束时,process.exitCode属性可以被设置,以便于告知进程使用哪个结束状态码。

如下例子说明了一个 错误使用 process.exit()方法的场景,会导致输出到stdout的数据清空或丢失:

// This is an example of what *not* to do:
if (someConditionNotMet()) {
  printUsageToStdout();
  process.exit(1);
}

这个例子中出现问题的原因在于,Node.js中写入到process.stdout的操作有时是异步的,并可能在Node.js事件轮询的多个ticks中出现。 调用process.exit()会使得在写入stdout的额外操作执行之前,进程就被强制结束了。

与直接调用process.exit()相比,代码应该设置process.exitCode并允许进程自然的结束,以免事件轮询队列中存在额外的工作:

// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
  printUsageToStdout();
  process.exitCode = 1;
}

如果出现错误情况,而有必要结束Node.js进程,抛出一个uncaught错误并且允许进程正常结束的处理方式,要比调用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.

To exit with a 'failure' code:

process.exit(1);

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

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().

In Worker threads, this function stops the current thread rather than the current process.