退出码


当没有更多异步操作挂起时,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 的消息到标准错误。
  • 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.