File system

FileHandle.close()

Releases an open file handle after pending operations complete. Tested on Node.js 22.16.0. Use finally for explicit cleanup instead of waiting for garbage collection.

Formal Syntax & Structure

nodejs syntaxnodejs
filehandle.close()

Return Value

Promise<void>

Resolves when the handle closes.

Working Example

Example Snippetnodejs
import * as fs from 'node:fs/promises';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir=await fs.mkdtemp(join(tmpdir(),'bytemind-ref-'));try {  const handle=await fs.open(join(dir,'data.txt'),'w');  try { await handle.writeFile('done'); } finally { await handle.close(); }  console.log(await fs.readFile(join(dir,'data.txt'),'utf8'));} finally { await fs.rm(dir,{recursive:true,force:true}); }
Node.js Reference