Skip to main content
Bun 中文文档 home page
Search...
⌘K
安装 Bun
Search...
Navigation
概述
指南
运行时
包管理器
打包器
测试运行器
指南
参考
博客
反馈
概述
指南
部署
在 Vercel 上部署
在 Railway 上部署
在 Render 上部署
在 AWS Lambda 上部署
在 DigitalOcean 上部署
在 Google Cloud Run 上部署
运行时与调试
TypeScript 类型
重新映射导入路径
VS Code 调试器
网页调试器
堆快照
构建时常量
定义常量
GitHub Actions
macOS 上的代码签名
实用工具
升级 Bun
检测 Bun
获取 Bun 版本
密码哈希
生成 UUID
Base64 编码
Gzip 压缩
DEFLATE 压缩
转义 HTML
深度相等
休眠
文件 URL 转路径
路径转文件 URL
查找可执行文件路径
import.meta.dir
import.meta.file
import.meta.path
检查入口点
获取入口文件路径
生态系统与框架
Astro 与 Bun
使用 Bun 的 Discord.js
使用 Docker 的 Bun
Drizzle 与 Bun
Gel 与 Bun
Elysia 与 Bun
Express 与 Bun
Hono 与 Bun
Mongoose 与 Bun
使用 Bun 的 Neon Drizzle
Neon 无服务器 Postgres 与 Bun
Next.js 与 Bun
Nuxt 搭配 Bun
PM2 与 Bun
Prisma ORM 与 Bun
在 Bun 中使用 Prisma Postgres
Qwik 与 Bun
React 与 Bun
使用 Remix 和 Bun
使用 Bun 启动 TanStack Start
Bun 中使用 Sentry
SolidStart 与 Bun
使用 Bun 进行 SSR React
StricJS 与 Bun
SvelteKit 与 Bun
systemd 与 Bun
Vite 与 Bun
Bun 配合 Upstash
HTTP 与网络
使用 Bun 的 HTTP 服务器
使用 Bun 创建简单 HTTP 服务器
Bun 中的 Fetch
热重载 HTTP 服务器
启动 HTTP 服务器集群
配置 TLS
使用 fetch() 代理 HTTP 请求
流式文件响应
使用 FormData 通过 HTTP 上传文件
使用 unix 域套接字的 Fetch
使用迭代器流式传输
使用 Node.js 流
WebSocket
简单服务器
发布-订阅服务器
上下文数据
启用压缩
进程与系统
启动子进程
读取标准输出
读取 stderr
解析命令行参数
从 stdin 读取
派生子进程并使用 IPC 通信
监听 CTRL+C
操作系统信号
进程运行时间
运行 shell 命令
设置时区
设置环境变量
读取环境变量
包管理器
添加依赖
添加开发依赖
添加可选依赖
添加一个 peer 依赖
添加 Git 依赖
添加一个 tarball 依赖
使用别名安装
Bun 的 Workspaces
覆盖默认的 npm registry
配置作用域注册表
在 Bun 中使用 Azure Artifacts
使用 Bun 的 JFrog Artifactory
添加受信任的依赖
生成兼容 yarn 的锁文件
从 npm 迁移到 bun
配置 git 比较 Bun 的锁文件
在 GitHub Actions 中安装 Bun
测试运行器
运行测试
监听模式
从 Jest 迁移
模拟函数
监视方法
模拟系统时间
快照测试
更新快照
覆盖率报告
覆盖率阈值
并发测试通配符
跳过测试
待办测试
测试超时
提前中止
重新运行测试
Testing Library
DOM 测试
测试 Svelte
运行时与调试
VS Code 调试器
网页调试器
堆快照
构建时常量
定义常量
GitHub Actions
macOS 上的代码签名
模块系统
导入 JSON
导入 TOML
导入 YAML
导入 HTML
import.meta.dir
import.meta.file
import.meta.path
检查入口点
获取入口文件路径
文件系统
作为字符串读取
读取到 Buffer
读取到 Uint8Array
读取到 ArrayBuffer
读取 JSON 文件
获取 MIME 类型
检查文件存在
监视目录
以流读取
写字符串到文件
写入 Blob
写入响应
追加到文件
递增写入
写入流
写入到标准输出
写文件到标准输出
复制文件
删除文件
删除文件
删除目录
实用工具
密码哈希
生成 UUID
Base64 编码
Gzip 压缩
DEFLATE 压缩
转义 HTML
深度相等
休眠
文件 URL 转路径
路径转文件 URL
查找可执行文件路径
HTML 处理
使用 HTMLRewriter 提取链接
OpenGraph 标签
二进制数据
ArrayBuffer 到字符串
ArrayBuffer 转 Buffer
ArrayBuffer 转 Blob
ArrayBuffer 转数组
ArrayBuffer 到 Uint8Array
Buffer 转字符串
Buffer 转 ArrayBuffer
Buffer 转 Blob
Buffer 转 Uint8Array
Buffer 转 ReadableStream
Blob 转字符串
Blob 转 ArrayBuffer
Blob 转 Uint8Array
Blob 转 DataView
Blob 转 ReadableStream
Uint8Array 转字符串
Uint8Array 到 ArrayBuffer
Uint8Array 转 Buffer
Uint8Array 转 Blob
Uint8Array 到 DataView
Uint8Array 转 ReadableStream
DataView 转字符串
Streams
流转换为字符串
流转 JSON
流转换为 Blob
流转 Buffer
流到 ArrayBuffer
Stream 转 Uint8Array
流转数组
Readable 转字符串
Readable 转 JSON
Readable 转 Blob
Readable 转 Uint8Array
Readable 到 ArrayBuffer
概述
指南
Copy page
一系列使用 Bun 执行常见任务的代码示例和操作演练。
Copy page
Featured
Use Tanstack Start with Bun
View guide
Use Next.js with Bun
View guide
Build a frontend using Vite and Bun
View guide
Install TypeScript declarations for Bun
View guide
Write a simple HTTP server
View guide
Build a simple WebSocket server
View guide
All Guides
Deployment
Deploy Bun on Vercel
Deploy Bun on Railway
Deploy Bun on Render
Binary data
Convert an ArrayBuffer to an array of numbers
Convert an ArrayBuffer to a Blob
Convert an ArrayBuffer to a Buffer
Convert an ArrayBuffer to a string
Convert an ArrayBuffer to a Uint8Array
Convert a Blob to an ArrayBuffer
Convert a Blob to a DataView
Convert a Blob to a ReadableStream
Convert a Blob to a string
Convert a Blob to a Uint8Array
Convert a Buffer to an ArrayBuffer
Convert a Buffer to a blob
Convert a Buffer to a ReadableStream
Convert a Buffer to a string
Convert a Buffer to a Uint8Array
Convert a DataView to a string
Convert a Uint8Array to an ArrayBuffer
Convert a Uint8Array to a Blob
Convert a Uint8Array to a Buffer
Convert a Uint8Array to a DataView
Convert a Uint8Array to a ReadableStream
Convert a Uint8Array to a string
Ecosystem
Use Gel with Bun
Use Prisma ORM with Bun
Use Prisma Postgres with Bun
Create a Discord bot
Add Sentry to a Bun app
Use Drizzle ORM with Bun
Build a React app with Bun
Run Bun as a daemon with PM2
Build an app with Nuxt and Bun
Build an app with Qwik and Bun
Build an app with Astro and Bun
Build an app with Remix and Bun
Use TanStack Start with Bun
Run Bun as a daemon with systemd
Build an app with Next.js and Bun
Build an app with SvelteKit and Bun
Build a frontend using Vite and Bun
Build an app with SolidStart and Bun
Use Neon Postgres through Drizzle ORM
Build an HTTP server using Hono and Bun
Use Neon's Serverless Postgres with Bun
Build an HTTP server using Elysia and Bun
Containerize a Bun application with Docker
Build an HTTP server using Express and Bun
Server-side render (SSR) a React component
Build an HTTP server using StricJS and Bun
Read and write data to MongoDB using Mongoose and Bun
HTMLRewriter
Extract links from a webpage using HTMLRewriter
Extract social share images and Open Graph tags
HTTP
Common HTTP server usage
Hot reload an HTTP server
Write a simple HTTP server
Start a cluster of HTTP servers
Configure TLS on an HTTP server
Send an HTTP request using fetch
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
Streaming HTTP Server with Node.js Streams
Package manager
Add a dependency
Add a Git dependency
Add a peer dependency
Add a tarball dependency
Add a trusted dependency
Add an optional dependency
Add a development dependency
Using bun install with Artifactory
Generate a yarn-compatible lockfile
Migrate from npm install to bun install
Configuring a monorepo using workspaces
Install a package under a different name
Configure git to diff Bun's lockb lockfile
Install dependencies with Bun in GitHub Actions
Override the default npm registry for bun install
Using bun install with an Azure Artifacts npm registry
Configure a private registry for an organization scope with bun install
Processes
Read from stdin
Listen for CTRL+C
Listen to OS signals
Spawn a child process
Parse command-line arguments
Read stderr from a child process
Read stdout from a child process
Get the process uptime in nanoseconds
Spawn a child process and communicate using IPC
Reading files
Read a JSON file
Check if a file exists
Read a file to a Buffer
Read a file as a string
Get the MIME type of a file
Read a file to a Uint8Array
Read a file to an ArrayBuffer
Watch a directory for changes
Read a file as a ReadableStream
Runtime
Delete files
Delete directories
Import a JSON file
Import a TOML file
Import a YAML file
Run a Shell Command
Re-map import paths
Set a time zone in Bun
Set environment variables
Import a HTML file as text
Read environment variables
Build-time constants with --define
Debugging Bun with the web debugger
Install and run Bun in GitHub Actions
Install TypeScript declarations for Bun
Debugging Bun with the VS Code extension
Inspect memory usage using V8 heap snapshots
Define and replace static globals & constants
Codesign a single-file JavaScript executable on macOS
Streams
Convert a ReadableStream to JSON
Convert a Node.js Readable to JSON
Convert a ReadableStream to a Blob
Convert a Node.js Readable to a Blob
Convert a ReadableStream to a Buffer
Convert a ReadableStream to a string
Convert a Node.js Readable to a string
Convert a ReadableStream to a Uint8Array
Convert a ReadableStream to an ArrayBuffer
Convert a Node.js Readable to an Uint8Array
Convert a Node.js Readable to an ArrayBuffer
Convert a ReadableStream to an array of chunks
Test runner
Mock functions in bun test
Spy on methods in bun test
Using Testing Library with Bun
Update snapshots in bun test
Run tests in watch mode with Bun
Use snapshot testing in bun test
Bail early with the Bun test runner
Skip tests with the Bun test runner
Migrate from Jest to Bun's test runner
Run your tests with the Bun test runner
Set the system time in Bun's test runner
Write browser DOM tests with Bun and happy-dom
Set a per-test timeout with the Bun test runner
Mark a test as a "todo" with the Bun test runner
Re-run tests multiple times with the Bun test runner
Set a code coverage threshold with the Bun test runner
Selectively run tests concurrently with glob patterns
Generate code coverage reports with the Bun test runner
import, require, and test Svelte components with bun test
Utilities
Hash a password
Generate a UUID
Escape an HTML string
Get the current Bun version
Encode and decode base64 strings
Check if two objects are deeply equal
Detect when code is executed with Bun
Get the directory of the current file
Get the file name of the current file
Convert a file URL to an absolute path
Compress and decompress data with gzip
Convert an absolute path to a file URL
Get the path to an executable bin file
Sleep for a fixed number of milliseconds
Compress and decompress data with DEFLATE
Get the absolute path of the current file
Check if the current file is the entrypoint
Get the absolute path to the current entrypoint
WebSocket
Build a simple WebSocket server
Enable compression for WebSocket messages
Build a publish-subscribe WebSocket server
Set per-socket contextual data on a WebSocket
Writing files
Delete a file
Write to stdout
Write a Blob to a file
Write a file to stdout
Append content to a file
Write a string to a file
Write a file incrementally
Write a Response to a file
Copy a file to another location
Write a ReadableStream to a file
在 Vercel 上部署 Bun 应用
Next
⌘I