FileHandle.writer method | Node.js fs/promises module | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:fs/promises/
FileHandle/
writerMwriter
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:fs/promises/
FileHandle/
writerMwriter
method
fs.promises.FileHandle.writerwriter(options?:
WriterOptions):
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 (no end() called), asyncDispose calls fail(). If end() is pending, it waits for it to complete.using w = fh.writer() — calls fail() 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 typesinterface
WriterOptionsautoClose?: boolean
Close the file handle when the writer ends or fails.
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 chunkSize for optimal pipeTo() performance.
limit?: number
Maximum number of bytes the writer will accept. Async writes (write(), writev()) that would exceed the limit reject with ERR_OUT_OF_RANGE. Sync writes (writeSync(), writevSync()) return false.
start?: number
Byte offset to start writing at. When specified, writes use explicit positioning.
interface
Writerreadonly
desiredSize: null | number
[Symbol.asyncDispose](): PromiseLikevoid>;
[Symbol.dispose](): void;
end(options?:
WriteOptions): Promisenumber>;
endSync(): number;
fail(reason?: any): void;
write(chunk: string |
Uint8ArrayArrayBufferLike>,options?:
WriteOptions): Promisevoid>;
writeSync(chunk: string |
Uint8ArrayArrayBufferLike>): boolean;
writev(chunks: string |
Uint8ArrayArrayBufferLike>[],options?:
WriteOptions): Promisevoid>;
writevSync(chunks: string |
Uint8ArrayArrayBufferLike>[]): boolean;
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →