--- name: bun-runtime description: | Bun JavaScript runtime, bundler, and package manager. Covers Bun.serve() HTTP server, Bun.file() I/O, SQLite, password hashing, Bun.build() bundler, bun:test runner, and package management. Use when building with Bun APIs, running scripts with Bun, bundling code, managing packages with bun install/add, or writing tests with bun:test. license: MIT metadata: author: oakoss version: '1.1' source: https://bun.sh/docs user-invocable: false --- # Bun Runtime ## Overview Bun is an all-in-one JavaScript and TypeScript runtime that includes a fast package manager, bundler, test runner, and Node.js-compatible APIs. It natively executes TypeScript and JSX without a separate compilation step. **When to use:** Fast server-side JavaScript, TypeScript-first projects, replacing Node.js for better startup performance, built-in SQLite, password hashing, file I/O, HTTP servers, bundling, and testing without external tooling. **When NOT to use:** Projects requiring full Node.js ecosystem compatibility (some native modules unsupported), production environments needing battle-tested stability of Node.js, or browser-only code that does not need a runtime. ## Quick Reference | Pattern | API | Key Points | | ---------------- | ----------------------------------------- | -------------------------------------------------------- | | HTTP server | `Bun.serve({ routes, fetch })` | Route-based, static/dynamic routes, per-method handlers | | File read | `Bun.file(path)` | Lazy BunFile (Blob), `.text()`, `.json()`, `.stream()` | | File write | `Bun.write(dest, data)` | Accepts string, Blob, Response, BunFile | | SQLite | `new Database(path)` from `bun:sqlite` | Synchronous queries, prepared statements, WAL mode | | Password hash | `Bun.password.hash(pw)` | Argon2id default, bcrypt option, async and sync variants | | Password verify | `Bun.password.verify(pw, hash)` | Auto-detects algorithm from hash format | | Bundler | `Bun.build({ entrypoints, outdir })` | Tree-shaking, code splitting, plugins, multiple targets | | Test runner | `import { test, expect } from "bun:test"` | Jest-compatible, mocking, snapshots, watch mode | | Install packages | `bun install` | Fast lockfile resolution, npm-compatible | | Add package | `bun add ` | `-d` for dev, `-g` for global | | Run script | `bun run