method
fs.promises.FileHandle.writer
Return a node:stream/iter writer backed by this file handle.
The writer supports both Symbol.asyncDispose and Symbol.dispose:
await using w = fh.writer()— if the writer is still open (noend()called),asyncDisposecallsfail(). Ifend()is pending, it waits for it to complete.using w = fh.writer()— callsfail()unconditionally.
The writeSync() and writevSync() methods enable the try-sync fast path used by stream/iter pipeTo(). When the reader's chunk size matches the writer's chunkSize, all writes in a pipeTo() pipeline complete synchronously with zero promise overhead.
This function is only available when the --experimental-stream-iter flag is enabled.
import { open } from 'node:fs/promises';
import { from, pipeTo } from 'node:stream/iter';
import { compressGzip } from 'node:zlib/iter';
// Async pipeline
const fh = await open('output.gz', 'w');
await pipeTo(from('Hello!'), compressGzip(), fh.writer({ autoClose: true }));
// Sync pipeline with limit
const src = await open('input.txt', 'r');
const dst = await open('output.txt', 'w');
const w = dst.writer({ limit: 1024 * 1024 }); // Max 1 MB
await pipeTo(src.pull({ autoClose: true }), w);
await w.end();
await dst.close();Referenced types
interface WriterOptions
- chunkSize?: number
Maximum chunk size in bytes for synchronous write operations. Writes larger than this threshold fall back to async I/O. Set this to match the reader's
chunkSizefor optimalpipeTo()performance. - limit?: number
Maximum number of bytes the writer will accept. Async writes (
write(),writev()) that would exceed the limit reject withERR_OUT_OF_RANGE. Sync writes (writeSync(),writevSync()) returnfalse.