弃用的 API


¥Deprecated APIs

Node.js API 可能由于以下任何原因而被弃用:

¥Node.js APIs might be deprecated for any of the following reasons:

  • 该 API 的使用是不安全的。

    ¥Use of the API is unsafe.

  • 改进的替代 API 可用。

    ¥An improved alternative API is available.

  • 预计在未来的主要版本中会对 API 进行重大更改。

    ¥Breaking changes to the API are expected in a future major release.

Node.js 使用四种弃用:

¥Node.js uses four kinds of deprecations:

  • 仅文档

    ¥Documentation-only

  • 应用(仅限非 node_modules 代码)

    ¥Application (non-node_modules code only)

  • 运行时(所有代码)

    ¥Runtime (all code)

  • 生命结束

    ¥End-of-Life

仅文档弃用是仅在 Node.js API 文档中表达的弃用。这些在运行 Node.js 时不会产生副作用。某些仅文档的弃用在使用 --pending-deprecation 标志(或其替代的 NODE_PENDING_DEPRECATION=1 环境变量)启动时会触发运行时警告,类似于下面的运行时弃用。支持该标志的仅文档弃用在 弃用的 API 列表 中明确标记为此类。

¥A Documentation-only deprecation is one that is expressed only within the Node.js API docs. These generate no side-effects while running Node.js. Some Documentation-only deprecations trigger a runtime warning when launched with --pending-deprecation flag (or its alternative, NODE_PENDING_DEPRECATION=1 environment variable), similarly to Runtime deprecations below. Documentation-only deprecations that support that flag are explicitly labeled as such in the list of Deprecated APIs.

默认情况下,仅针对非 node_modules 代码的应用弃用将生成一个进程警告,当首次在未从 node_modules 加载的代码中使用已弃用的 API 时,该警告将打印到 stderr。当使用 --throw-deprecation 命令行标志时,运行时弃用将导致抛出错误。当使用 --pending-deprecation 时,对于从 node_modules 加载的代码也会触发警告。

¥An Application deprecation for only non-node_modules code will, by default, generate a process warning that will be printed to stderr the first time the deprecated API is used in code that's not loaded from node_modules. When the --throw-deprecation command-line flag is used, a Runtime deprecation will cause an error to be thrown. When --pending-deprecation is used, warnings will also be emitted for code loaded from node_modules.

所有代码的运行时弃用与非 node_modules 代码的运行时弃用类似,不同之处在于它还会对从 node_modules 加载的代码触发警告。

¥A runtime deprecation for all code is similar to the runtime deprecation for non-node_modules code, except that it also emits a warning for code loaded from node_modules.

当特性已经或即将从 Node.js 中删除时,使用生命结束的弃用。

¥An End-of-Life deprecation is used when functionality is or will soon be removed from Node.js.