skills/bun-guides-write-file-basic/SKILL.md
Write a string to a file
npx skillsauth add jarle/bun-skills Bun Write a string to a fileInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This code snippet writes a string to disk at a particular absolute path.
It uses the fast Bun.write() API to efficiently write data to disk. The first argument is a destination; the second is the data to write.
const path = "/path/to/file.txt";
await Bun.write(path, "Lorem ipsum");
Any relative paths will be resolved relative to the project root (the nearest directory containing a package.json file).
const path = "./file.txt";
await Bun.write(path, "Lorem ipsum");
You can pass a BunFile as the destination. Bun.write() will write the data to its associated path.
const path = Bun.file("./file.txt");
await Bun.write(path, "Lorem ipsum");
Bun.write() returns the number of bytes written to disk.
const path = "./file.txt";
const bytes = await Bun.write(path, "Lorem ipsum");
// => 11
See Docs > API > File I/O for complete documentation of Bun.write().
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling