[ PROMPT_NODE_23786 ]
bun-development
[ SKILL_DOCUMENTATION ]
# ⚡ Bun 开发
> 基于 [oven-sh/bun](https://github.com/oven-sh/bun) 的快速、现代 JavaScript/TypeScript 开发,使用 Bun 运行时。
## 何时使用此技能
在以下场景使用此技能:
- 使用 Bun 启动新的 JS/TS 项目
- 从 Node.js 迁移到 Bun
- 优化开发速度
- 使用 Bun 的内置工具(打包器、测试运行器)
- 排查 Bun 特有的问题
---
## 1. 入门指南
### 1.1 安装
bash
# macOS / Linux
curl -fsSL https://bun.sh/install | bash
# Windows
powershell -c "irm bun.sh/install.ps1 | iex"
# 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 创建新项目
bash
# 初始化项目
bun init
# 创建内容:
# ├── package.json
# ├── tsconfig.json
# ├── index.ts
# └── README.md
# 使用特定模板
bun create
# 示例
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"
}
}