DEP0200:在垃圾回收时关闭 fs.Dir


【DEP0200: Closing fs.Dir on garbage collection】

类型:仅文档

【Type: Documentation-only】

允许在垃圾回收时关闭 fs.Dir 对象已被弃用。将来,这样做可能会导致抛出错误,从而终止进程。

【Allowing a fs.Dir object to be closed on garbage collection is deprecated. In the future, doing so might result in a thrown error that will terminate the process.】

请确保所有 fs.Dir 对象都使用 Dir.prototype.close()using 关键字显式关闭:

【Please ensure that all fs.Dir objects are explicitly closed using Dir.prototype.close() or using keyword:】

import { opendir } from 'node:fs/promises';

{
  await using dir = await opendir('/async/disposable/directory');
} // Closed by dir[Symbol.asyncDispose]()

{
  using dir = await opendir('/sync/disposable/directory');
} // Closed by dir[Symbol.dispose]()

{
  const dir = await opendir('/unconditionally/iterated/directory');
  for await (const entry of dir) {
    // process an entry
  } // Closed by iterator
}

{
  let dir;
  try {
    dir = await opendir('/legacy/closeable/directory');
  } finally {
    await dir?.close();
  }
}