Filesystem

FileHandle.sync()

Requests synchronization of file contents and metadata. Durability of an entire rename workflow can also require syncing the containing directory on supported systems. Tested on Node.js 22.16.0. Example output (one line per entry): ["updated"].

Formal Syntax & Structure

nodejs syntaxnodejs
handle.sync()

Return Value

Promise<void>

Completes after sync.

Working Example

Example Snippetnodejs
import * as fs from 'node:fs/promises';import { constants } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = await fs.mkdtemp(join(tmpdir(), 'bytemind-'));try {    const p = join(dir, 'book.txt');    await fs.writeFile(p, 'catalog');    const h = await fs.open(p, 'r+');    try {        await h.writeFile('updated');        await h.sync();    }    finally {        await h.close();    }    console.log(await fs.readFile(p, 'utf8'));}finally {    await fs.rm(dir, { recursive: true, force: true });}
Node.js Reference