退出码


当没有异步的操作在等待处理时,则 Node.js 会以状态码 0 正常地退出。 其他情况下,会用如下的状态码:

  • 1 未捕获的致命异常:有未捕获的异常,并且没被域或 'uncaughtException' 事件句柄处理。
  • 2:未被使用(被 Bash 预留用于内置的误用)。
  • 3 内部的 JavaScript 解析错误:Node.js 引导进程内部中的 JavaScript 源代码造成的解析错误。 这是非常少见的, 一般只会在开发 Node.js 本身的时候出现。
  • 4 内部的 JavaScript 执行失败:Node.js 引导进程内部中的 JavaScript 源代码在执行时返回函数值失败。 这是非常少见的, 一般只会在开发 Node.js 本身的时候出现。
  • 5 致命的错误:在 V8 中有致命的错误。 通常会以 FATAL ERROR 为前缀打印消息到 stderr。
  • 6 非函数的内部异常句柄:有未捕获的异常,但是内部的致命异常句柄被设置为非函数且不能被调用。
  • 7 内部异常句柄运行时失败:有未捕获的异常,且内部的致命异常句柄函数在试图处理异常时本身抛出了错误。 这是可能发生的,比如,'uncaughtException' 或者 domain.on('error') 句柄抛出错误。
  • 8:未被使用。在之前的 Node.js 版本中,退出码 8 有时候表示未被捕获的异常。
  • 9 无效的参数:指定了未知的选项,或者没给必需要的选项提供值。
  • 10 内部的 JavaScript 运行时失败:当引导函数被调用时,Node.js 引导进程内部中的 JavaScript 源代码抛出错误。 这是非常少见的, 一般只会在开发 Node.js 本身的时候出现。
  • 12 无效的调试参数--inspect 和/或 --inspect-brk 选项已设置,但选择的端口号无效或不可用。
  • 13 未完成的顶层等待: await 被用于顶层代码中的函数之外,但是传递的 Promise 从未被解决。
  • >128 信号退出:如果 Node.js 接收到致命信号, 比如 SIGKILLSIGHUP,则其退出码会是 128 加上信号码的值。 这是标准 POSIX 的实践,因为退出码被定义为 7 位整数,并且信号退出设置了高位,然后包含信号码的值。 例如,信号 SIGABRT 的值为 6,因此期望的退出码会是 128 + 6134

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 the Node.js 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 the Node.js 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 an '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 the Node.js 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 --inspect and/or --inspect-brk options were set, but the port number chosen was invalid or unavailable.
  • 13 Unfinished Top-Level Await: await was used outside of a function in the top-level code, but the passed Promise never resolved.
  • >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 POSIX 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. For example, signal SIGABRT has value 6, so the expected exit code will be 128 + 6, or 134.