弃用的 API
【Deprecated APIs】
Node.js API 可能由于以下任何原因而被弃用:
【Node.js APIs might be deprecated for any of the following reasons:】
- 使用该 API 不安全。
- 有一个改进的替代 API 可用。
- 预计在未来的主要版本中,API将会有重大变更。
Node.js 使用四种弃用:
【Node.js uses four kinds of deprecations:】
- 仅限文档
- 应用(仅限非
node_modules代码) - 运行时(所有代码)
- 生命终结
仅文档弃用是指仅在 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.】