Guides
Code samples and walkthroughs for common tasks with Bun
Overview
1Deployment
6Deploy a Bun application on Vercel
Deploy a Bun application on Railway
Deploy a Bun application to Railway from the CLI or dashboard, with optional PostgreSQL setup and automatic SSL
Deploy a Bun application on Render
Deploy a Bun application on AWS Lambda
Deploy a Bun application on DigitalOcean
Deploy a Bun application on Google Cloud Run
Runtime & Debugging
9Install TypeScript declarations for Bun
Re-map import paths
Debugging Bun with the VS Code extension
Debugging Bun with the web debugger
Inspect memory usage using V8 heap snapshots
Build-time constants with --define
Define and replace static globals & constants
Install and run Bun in GitHub Actions
Codesign a single-file JavaScript executable on macOS
Fix the "can't be opened because it is from an unidentified developer" Gatekeeper warning when running your JavaScript executable.
Utilities
19Upgrade Bun to the latest version
Detect when code is executed with Bun
Get the current Bun version
Hash a password
Generate a UUID
Encode and decode base64 data
Compress and decompress data with gzip
Compress and decompress data with DEFLATE
Escape an HTML string
Check if two objects are deeply equal
Sleep for a fixed number of milliseconds
Convert a file URL to an absolute path
Convert an absolute path to a file URL
Get the path to an executable bin file
Get the directory of the current file
Get the file name of the current file
Get the absolute path of the current file
Check if the current file is the entrypoint
Get the absolute path to the current entrypoint
Ecosystem & Frameworks
27Build an app with Astro and Bun
Create a Discord bot
Containerize a Bun application with Docker
Use Drizzle ORM with Bun
Use Gel with Bun
Build an HTTP server using Elysia and Bun
Build an HTTP server using Express and Bun
Build an HTTP server using Hono and Bun
Read and write data to MongoDB using Mongoose and Bun
Use Neon Postgres through Drizzle ORM
Use Neon's Serverless Postgres with Bun
Build an app with Next.js and Bun
Build an app with Nuxt and Bun
Run Bun as a daemon with PM2
Use Prisma with Bun
Use Prisma Postgres with Bun
Build an app with Qwik and Bun
Build a React app with Bun
Build an app with Remix and Bun
Use TanStack Start with Bun
Add Sentry to a Bun app
Build an app with SolidStart and Bun
Server-side render (SSR) a React component
Build an app with SvelteKit and Bun
Run Bun as a daemon with systemd
Build a frontend using Vite and Bun
Bun Redis with Upstash
HTTP & Networking
13Common HTTP server usage
Write a simple HTTP server
Send an HTTP request using fetch
Hot reload an HTTP server
Start a cluster of HTTP servers
Run multiple HTTP servers concurrently with the "reusePort" option to share the same port across multiple processes
Configure TLS on an HTTP server
Proxy HTTP requests using fetch()
Stream a file as an HTTP Response
Upload files via HTTP using FormData
fetch with unix domain sockets in Bun
Streaming HTTP Server with Async Iterators
Server-Sent Events (SSE) with Bun
Streaming HTTP Server with Node.js Streams
WebSocket
4Processes & System
13Spawn a child process
Read stdout from a child process
Read stderr from a child process
Parse command-line arguments
Read from stdin
Spawn a child process and communicate using IPC
Listen for CTRL+C
Listen to OS signals
Get the process uptime in nanoseconds
Run a Shell Command
Set a time zone in Bun
Set environment variables
Read environment variables
Package Manager
17Add a dependency
Add a development dependency
Add an optional dependency
Add a peer dependency
Add a Git dependency
Add a tarball dependency
Install a package under a different name
Configuring a monorepo using workspaces
Override the default npm registry for bun install
Configure a private registry for an organization scope with bun install
Using bun install with an Azure Artifacts npm registry
Using bun install with Artifactory
Add a trusted dependency
Generate a yarn-compatible lockfile
Migrate from npm install to bun install
Configure git to diff Bun's lockb lockfile
Install dependencies with Bun in GitHub Actions
Test Runner
19Run your tests with the Bun test runner
Run tests in watch mode with Bun
Migrate from Jest to Bun's test runner
Mock functions in `bun test`
Spy on methods in `bun test`
Set the system time in Bun's test runner
Use snapshot testing in `bun test`
Update snapshots in `bun test`
Generate code coverage reports with the Bun test runner
Set a code coverage threshold with the Bun test runner
Selectively run tests concurrently with glob patterns
Set a glob pattern to decide which tests from which files run in parallel
Skip tests with the Bun test runner
Mark a test as a "todo" with the Bun test runner
Set a per-test timeout with the Bun test runner
Bail early with the Bun test runner
Re-run tests multiple times with the Bun test runner
Using Testing Library with Bun
Write browser DOM tests with Bun and happy-dom
import, require, and test Svelte components with bun test
Module System
6File System
21Read a file as a string
Read a file to a Buffer
Read a file to a Uint8Array
Read a file to an ArrayBuffer
Read a JSON file
Get the MIME type of a file
Check if a file exists
Watch a directory for changes
Read a file as a ReadableStream
Write a string to a file
Write a Blob to a file
Write a Response to a file
Append content to a file
Write a file incrementally
Write a ReadableStream to a file
Write to stdout
Write a file to stdout
Copy a file to another location
Delete a file
Delete files
Delete directories
HTML Processing
2Binary Data
22Convert an ArrayBuffer to a string
Convert an ArrayBuffer to a Buffer
Convert an ArrayBuffer to a Blob
Convert an ArrayBuffer to an array of numbers
Convert an ArrayBuffer to a Uint8Array
Convert a Buffer to a string
Convert a Buffer to an ArrayBuffer
Convert a Buffer to a blob
Convert a Buffer to a Uint8Array
Convert a Buffer to a ReadableStream
Convert a Blob to a string
Convert a Blob to an ArrayBuffer
Convert a Blob to a Uint8Array
Convert a Blob to a DataView
Convert a Blob to a ReadableStream
Convert a Uint8Array to a string
Convert a Uint8Array to an ArrayBuffer
Convert a Uint8Array to a Buffer
Convert a Uint8Array to a Blob
Convert a Uint8Array to a DataView
Convert a Uint8Array to a ReadableStream
Convert a DataView to a string
Streams
12Convert a ReadableStream to a string
Convert a ReadableStream to JSON
Convert a ReadableStream to a Blob
Convert a ReadableStream to a Buffer
Convert a ReadableStream to an ArrayBuffer
Convert a ReadableStream to a Uint8Array
Convert a ReadableStream to an array of chunks
Convert a Node.js Readable to a string
Convert a Node.js Readable to JSON
Convert a Node.js Readable to a Blob
Convert a Node.js Readable to an Uint8Array
Convert a Node.js Readable to an ArrayBuffer