fs.appendFileSync synchronous file append
The fs.appendFileSync function appends data to a file synchronously. It can be imported from node:fs. An optional encoding parameter can be specified, such as 'utf8': appendFileSync('message.txt', 'data to append', 'utf8'). The content can be a string or a Buffer.
Bun implements node:fs module
Bun implements the node:fs module, which includes the fs.appendFile and fs.appendFileSync functions for appending content to files.
fs.appendFile Promise-based example
import { appendFile } from "node:fs/promises";
await appendFile("message.txt", "data to append");
fs.appendFile callback-based example
import { appendFile } from "node:fs";
appendFile("message.txt", "data to append", err => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Write a file to stdout using Bun.write()
To write a file to stdout like the Unix cat command, use Bun.file() to create a file object and pass it as the second argument to Bun.write() with Bun.stdout as the first argument: await Bun.write(Bun.stdout, file).
Writing file contents with Bun.write() example
const path = "./out.txt";
const data = Bun.file("./in.txt");
await Bun.write(path, data);
BunFile extends Blob
The BunFile class extends Blob, so a BunFile instance can be passed directly to Bun.write() as the data argument to write file contents to disk.
Bun.write() signature for writing Blob to file
Bun.write() accepts a destination (string path or BunFile instance) as the first argument and data (Blob or string) as the second argument, and returns a Promise. Example: await Bun.write(path, "Lorem ipsum") or await Bun.write(path, blobData).
Bun.write() accepts BunFile as source
Bun.write() can accept a BunFile instance as the second argument to copy file contents to a destination path or BunFile instance.
Copy a file using Bun.write()
To copy a file to another location on disk, use Bun.write() where the first argument is a destination (an absolute path or BunFile instance) and the second argument is the file to copy. Example: const file = Bun.file("/path/to/original.txt"); await Bun.write("/path/to/copy.txt", file);
Example: writing fetched Response to file with Bun.write()
const result = await fetch("https://bun.com");
const path = "./file.txt";
await Bun.write(path, result);
Bun.write() can write Response objects to disk
Use Bun.write() to write a Response to disk. The body of the Response is written to the destination file. The first argument is a destination (an absolute path string or BunFile instance). The second argument is the data to write (which can be a Response object).
Write to stdout using Bun.write()
To write to stdout using Bun.write(), pass Bun.stdout as the destination: await Bun.write(Bun.stdout, "text");
Bun.stdout property exposes stdout as BunFile
Bun exposes stdout as a BunFile through the Bun.stdout property. This BunFile can be passed as the destination parameter to Bun.write().
BunFile.writer() returns FileSink
Call .writer() on a BunFile to retrieve a FileSink instance for incrementally writing to a file.
FileSink.write() accepts strings and binary data
The .write() method on FileSink accepts strings, Buffer objects, or Uint8Array instances.
FileSink.flush() writes buffer to disk
Call .flush() on FileSink to write the buffer to disk. You can write and flush multiple times before closing the file.
FileSink.end() closes file after flushing
Call .end() on FileSink to flush the buffer and close the file when writing is complete.
FileSink auto-flushes when buffer is full
FileSink automatically flushes when its internal buffer reaches capacity, in addition to manual flush() calls.
FileSink highWaterMark option configures buffer size
When creating a FileSink with file.writer(), pass a highWaterMark option to configure the buffer size in bytes. Example: file.writer({ highWaterMark: 1024 * 1024 }) sets a 1MB buffer.
Incremental file writing with FileSink example
const file = Bun.file("/path/to/file.txt");
const writer = file.writer();
writer.write("lorem");
writer.write("ipsum");
writer.write("dolor");
writer.flush();
Write ReadableStream to file with Bun.write()
To write a ReadableStream to disk, create a Response from the stream and pass it to Bun.write(). The example shows: const stream: ReadableStream = ...; const path = "./file.txt"; const response = new Response(stream); await Bun.write(path, response);
Delete a file with Bun.file().delete()
To delete a file using Bun, call Bun.file() with the file path to get a BunFile instance, then call the .delete() method on it. The .delete() method is asynchronous and returns a promise that resolves when the file has been deleted.
Example: Delete a file with Bun
const path = "/path/to/file.txt";
const file = Bun.file(path);
await file.delete();