
关于
使用 Bun 运行时进行快速、现代的 JavaScript/TypeScript 开发。
name: bun-development description: "使用 Bun 运行时进行快速、现代的 JavaScript/TypeScript 开发,灵感来自 oven-sh/bun。" risk: critical source: community date_added: "2026-02-27"
Bun 开发
使用 Bun 运行时进行快速、现代的 JavaScript/TypeScript 开发,灵感来自 oven-sh/bun。
何时使用此技能
使用此技能当:
- 使用 Bun 启动新的 JS/TS 项目
- 从 Node.js 迁移到 Bun
- 优化开发速度
- 使用 Bun 的内置工具(打包器、测试运行器)
- 排查 Bun 特定问题
1. 入门
1.1 安装
# macOS / Linux
brew install oven-sh/bun/bun
# 替代方案:下载官方安装程序,检查后执行
curl -fsSLo /tmp/bun-install.sh https://bun.sh/install
sed -n '1,160p' /tmp/bun-install.sh
bash /tmp/bun-install.sh
# Windows
powershell -NoProfile -Command "Invoke-WebRequest https://bun.sh/install.ps1 -OutFile $env:TEMP\\bun-install.ps1; Get-Content $env:TEMP\\bun-install.ps1 -TotalCount 120; powershell -ExecutionPolicy Bypass -File $env:TEMP\\bun-install.ps1"
# Homebrew
brew tap oven-sh/bun
brew install bun
# npm(如需要)
npm install -g bun
# 升级
bun upgrade
1.2 为什么选择 Bun?
| 特性 | Bun | Node.js | | :-------------- | :------------- | :-------------------------- | | 启动时间 | ~25ms | ~100ms+ | | 包安装 | 快10-100倍 | 基准 | | TypeScript | 原生 | 需要转译器 | | JSX | 原生 | 需要转译器 | | 测试运行器 | 内置 | 外部(Jest, Vitest) | | 打包器 | 内置 | 外部(Webpack, esbuild) |
2. 项目设置
2.1 创建新项目
# 初始化项目
bun init
# 创建:
# ├── package.json
# ├── tsconfig.json
# ├── index.ts
# └── README.md
# 使用特定模板
bun create <template> <project-name>
# 示例
bun create react my-app # React 应用
bun create next my-app # Next.js 应用
bun create vite my-app # Vite 应用
bun create elysia my-api # Elysia API
2.2 package.json
{
"name": "my-bun-project",
"version": "1.0.0",
"module": "index.ts",
"type": "module",
"scripts": {
"dev": "bun run --watch index.ts",
"start": "bun run index.ts",
"test": "bun test",
"build": "bun build ./index.ts --outdir ./dist",
"lint": "bunx eslint ."
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
2.3 tsconfig.json(Bun优化)
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": ["bun-types"]
}
}
3. 包管理
3.1 安装包
# 从 package.json 安装
bun install # 或 'bun i'
# 添加依赖
bun add express # 常规依赖
bun add -d typescript # 开发依赖
bun add -D @types/node # 开发依赖(别名)
bun add --optional pkg # 可选依赖
# 从特定注册表
bun add lodash --registry https://registry.npmmirror.com
# 安装特定版本
bun add react@18.2.0
bun add react@latest
bun add react@next
# 从 git
bun add github:user/repo
bun add git+https://github.com/user/repo.git
3.2 移除和更新
# 移除包
bun remove lodash
# 更新包
bun update # 更新全部
bun update lodash # 更新特定包
bun update --latest # 更新到最新(忽略范围)
# 检查过时包
bun outdated
3.3 bunx(npx 等价物)
# 执行包二进制文件
bunx prettier --write .
bunx tsc --init
bunx create-react-app my-app
# 使用特定版本
bunx -p typescript@4.9 tsc --version
# 不安装直接运行
bunx cowsay "Hello from Bun!"
3.4 锁文件
# bun.lockb 是二进制锁文件(更快解析)
# 生成文本锁文件用于调试:
bun install --yarn # 创建 yarn.lock
# 信任现有锁文件
bun install --frozen-lockfile
4. 运行代码
4.1 基本执行
# 直接运行 TypeScript(无需构建步骤!)
bun run index.ts
# 运行 JavaScript
bun run index.js
# 带参数运行
bun run server.ts --port 3000
# 运行 package.json 脚本
bun run dev
bun run build
# 简写形式(用于脚本)
bun dev
bun build
4.2 监视模式
# 文件变更时自动重启
bun --watch run index.ts
# 带热重载
bun --hot run index.ts
兼容工具
Claude CodeCursor
标签
后端开发
