process.exit([code])
code<integer> | <string> | <null> | <undefined> 退出代码。对于字符串类型,只允许整数字符串(例如:'1')。默认值:0。
process.exit() 方法指示 Node.js 以同步方式终止进程,并使用 code 作为退出状态。如果省略 code,退出将使用“成功”代码 0,或者如果已设置过 process.exitCode,则使用其值。Node.js 在所有 '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:】
import { exit } from 'node:process';
exit(1);const { exit } = require('node:process');
exit(1);执行 Node.js 的 shell 应该将退出码视为 1。
【The shell that executed Node.js should see the exit code as 1.】
调用 process.exit() 将强制进程尽快退出,即使仍有未完成的异步操作,包括对 process.stdout 和 process.stderr 的 I/O 操作。
【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.】
在大多数情况下,实际上没有必要显式调用 process.exit()。如果事件循环中没有待处理的额外工作,Node.js 进程会自动退出。可以设置 process.exitCode 属性来告诉进程在优雅退出时使用哪个退出代码。
【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.】
例如,下面的例子说明了 process.exit() 方法的 误用,这可能导致打印到标准输出的数据被截断和丢失:
【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:】
import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}const { exit } = require('node:process');
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}之所以这是一个问题,是因为在 Node.js 中写入 process.stdout 有时是异步的,并且可能会跨多个 Node.js 事件循环的周期进行。然而,调用 process.exit() 会强制进程在这些额外的 stdout 写入完成之前退出。
【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.】
与其直接调用 process.exit(),代码应该设置 process.exitCode 并通过避免为事件循环安排任何额外工作,让进程自然退出:
【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:】
import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}const process = require('node:process');
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}如果由于错误情况需要终止 Node.js 进程,抛出一个未捕获的错误并允许进程相应终止,比调用 process.exit() 更安全。
【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().】
在 Worker 线程中,此函数会停止当前线程而不是当前进程。
【In Worker threads, this function stops the current thread rather
than the current process.】