fs.appendFile()

Asynchronously appends data to a file, creating the file if it does not exist.

Since Node 10 (fs/promises) Spec ↗

Syntax

await appendFile(path, data[, options])

Parameters

NameTypeRequiredDescription
path string | Buffer | URL | integer Yes The filename or file descriptor.
data string | Buffer Yes The data to append.
options object | string No An encoding string or object with `encoding`, `mode`, and `flag`.

Returns

Promise<void> — Resolves when the append completes.

Examples

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

await appendFile('./app.log', `${new Date().toISOString()} started\n`);
import { appendFile } from 'node:fs/promises';

for (const line of ['a', 'b', 'c']) {
  await appendFile('./list.txt', line + '\n');
}

Notes

Creates the file if missing. Awaiting each append in a loop keeps lines ordered; firing them in parallel may interleave. For high-volume logging prefer a write stream opened once with flag `'a'`.

See also