process.exitCode
-
<integer> | <string> | <null> | <undefined> 退出码。对于字符串类型,仅允许整数字符串(例如,'1')。默认值:
undefined
。¥<integer> | <string> | <null> | <undefined> The exit code. For string type, only integer strings (e.g.,'1') are allowed. Default:
undefined
.
当进程正常退出或通过 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.
可以通过将值分配给 process.exitCode
或将参数传递给 process.exit()
来更新 process.exitCode
的值:
¥The value of process.exitCode
can be updated by either assigning a value to
process.exitCode
or by passing an argument to process.exit()
:
$ node -e 'process.exitCode = 9'; echo $?
9
$ node -e 'process.exit(42)'; echo $?
42
$ node -e 'process.exitCode = 9; process.exit(42)'; echo $?
42
当发生不可恢复的错误(例如遇到未解决的顶层等待)时,Node.js 也可以隐式设置该值。但是,退出代码的显式操作始终优先于隐式操作:
¥The value can also be set implicitly by Node.js when unrecoverable errors occur (e.g. such as the encountering of an unsettled top-level await). However explicit manipulations of the exit code always take precedence over implicit ones:
$ node --input-type=module -e 'await new Promise(() => {})'; echo $?
13
$ node --input-type=module -e 'process.exitCode = 9; await new Promise(() => {})'; echo $?
9