DEP0137: 在垃圾回收时关闭 fs.FileHandle


类型: 运行时

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

请确保当不再需要 fs.FileHandle 时,使用 FileHandle.prototype.close() 显式关闭所有 fs.FileHandle 对象:

const fsPromises = require('fs').promises;
async function openAndClose() {
  let filehandle;
  try {
    filehandle = await fsPromises.open('thefile.txt', 'r');
  } finally {
    if (filehandle !== undefined)
      await filehandle.close();
  }
}

Type: Runtime

Allowing a fs.FileHandle 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.

Please ensure that all fs.FileHandle objects are explicitly closed using FileHandle.prototype.close() when the fs.FileHandle is no longer needed:

const fsPromises = require('fs').promises;
async function openAndClose() {
  let filehandle;
  try {
    filehandle = await fsPromises.open('thefile.txt', 'r');
  } finally {
    if (filehandle !== undefined)
      await filehandle.close();
  }
}