Bun CLI 命令 #
概述 #
Bun 提供了一套强大的命令行工具,涵盖运行代码、包管理、测试、打包等功能。
查看帮助 #
bash
# 查看所有命令
bun --help
# 查看特定命令帮助
bun run --help
bun test --help
bun build --help
查看版本 #
bash
bun --version
bun -v
bun run #
基本用法 #
bash
# 运行 JavaScript 文件
bun run index.js
# 运行 TypeScript 文件
bun run index.ts
# 运行 JSX 文件
bun run app.tsx
# 简写(省略 run)
bun index.ts
运行选项 #
bash
# 热重载模式
bun --hot run server.ts
# 监听文件变化
bun --watch run server.ts
# 设置环境变量
bun --env-file=.env.local run index.ts
# 指定工作目录
bun --cwd /path/to/project run index.ts
# 静默模式
bun --silent run script.ts
执行代码 #
bash
# 执行单行代码
bun -e "console.log('Hello')"
# 执行多行代码
bun -e "
const name = 'Bun';
console.log(\`Hello, \${name}!\`);
"
# 从 stdin 读取
echo "console.log('Hello')" | bun -
运行 package.json 脚本 #
json
{
"scripts": {
"start": "bun run src/index.ts",
"dev": "bun --hot run src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"test": "bun test",
"lint": "eslint src/"
}
}
bash
# 运行脚本
bun run start
bun run dev
bun run build
# 简写
bun start
bun dev
bun build
传递参数 #
bash
# 传递参数给脚本
bun run start --port 3000
# 在脚本中获取参数
// index.ts
const args = process.argv.slice(2);
console.log(args); // ['--port', '3000']
预加载文件 #
bash
# 预加载文件
bun --preload ./setup.ts run index.ts
# 在 bunfig.toml 中配置
[run]
preload = ["./setup.ts"]
bun install / bun add #
安装依赖 #
bash
# 安装所有依赖
bun install
# 简写
bun i
添加依赖 #
bash
# 添加生产依赖
bun add react
bun add react react-dom
# 添加开发依赖
bun add -d typescript
bun add --dev @types/node
# 添加可选依赖
bun add -O package-name
# 添加全局依赖
bun add -g prettier
bun add --global eslint
# 添加特定版本
bun add react@18
bun add react@18.2.0
bun add react@latest
安装选项 #
bash
# 精确版本
bun add --exact react
# 离线模式
bun install --offline
# 生产模式(不安装 devDependencies)
bun install --production
# 忽略脚本
bun install --ignore-scripts
# 强制安装
bun install --force
# 从 git 仓库安装
bun add github:user/repo
bun add git@github.com:user/repo.git
bun add https://github.com/user/repo.git
移除依赖 #
bash
# 移除依赖
bun remove react
# 移除多个
bun remove react react-dom
# 移除全局依赖
bun remove -g prettier
更新依赖 #
bash
# 更新所有依赖
bun update
# 更新特定依赖
bun update react
# 交互式更新
bun update --interactive
bun test #
运行测试 #
bash
# 运行所有测试
bun test
# 运行特定文件
bun test index.test.ts
# 运行匹配模式的测试
bun test --grep "api"
# 监听模式
bun test --watch
测试选项 #
bash
# 显示覆盖率
bun test --coverage
# 并行运行
bun test --jobs 4
# 超时设置
bun test --timeout 10000
# 只运行失败的测试
bun test --only-failures
# 更新快照
bun test --update-snapshots
# 详细输出
bun test --verbose
测试过滤 #
bash
# 运行特定目录的测试
bun test tests/api/
# 排除文件
bun test --exclude "node_modules/**"
# 包含模式
bun test --include "**/*.test.ts"
bun build #
基本打包 #
bash
# 基本打包
bun build ./src/index.ts --outdir ./dist
# 指定输出文件
bun build ./src/index.ts --outfile ./dist/bundle.js
# 打包为可执行文件
bun build ./src/index.ts --compile --outfile myapp
输出格式 #
bash
# ES 模块
bun build ./src/index.ts --outdir ./dist --format esm
# CommonJS
bun build ./src/index.ts --outdir ./dist --format cjs
# IIFE(立即执行函数)
bun build ./src/index.ts --outdir ./dist --format iife
打包选项 #
bash
# 压缩代码
bun build ./src/index.ts --outdir ./dist --minify
# 生成 Source Map
bun build ./src/index.ts --outdir ./dist --sourcemap
# 目标环境
bun build ./src/index.ts --outdir ./dist --target browser
bun build ./src/index.ts --outdir ./dist --target node
bun build ./src/index.ts --outdir ./dist --target bun
# 定义环境变量
bun build ./src/index.ts --outdir ./dist --define NODE_ENV=production
# 外部依赖
bun build ./src/index.ts --outdir ./dist --external react --external react-dom
# 代码分割
bun build ./src/index.ts --outdir ./dist --splitting
# 命名块
bun build ./src/index.ts --outdir ./dist --chunk-naming "[name]-[hash].js"
多入口打包 #
bash
# 多入口
bun build ./src/index.ts ./src/admin.ts --outdir ./dist
打包配置文件 #
typescript
// build.config.ts
export default {
entrypoints: ["./src/index.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: true,
sourcemap: "external",
splitting: true,
external: ["react", "react-dom"],
define: {
NODE_ENV: JSON.stringify("production"),
},
};
bash
bun build --config build.config.ts
bun x / bunx #
执行包命令 #
bash
# 执行 npm 包命令
bunx create-react-app my-app
bunx vite
# 使用特定版本
bunx create-react-app@5 my-app
# 从 git 仓库执行
bunx github:user/repo
与 npx 的区别 #
bash
# npx - 每次都检查/下载
npx create-react-app my-app
# bunx - 更快,使用 Bun 的包管理
bunx create-react-app my-app
bun create #
创建项目 #
bash
# 从模板创建
bun create react my-app
bun create next my-app
bun create vite my-app
# 使用官方模板
bun create bun my-app
# 从 GitHub 模板创建
bun create github:user/template-repo my-app
可用模板 #
bash
# 查看可用模板
bun create --help
# 常用模板
bun create react # React 应用
bun create next # Next.js 应用
bun create vite # Vite 项目
bun create hono # Hono 框架
bun create elysia # Elysia 框架
bun upgrade #
升级 Bun #
bash
# 升级到最新版本
bun upgrade
# 升级到 canary 版本
bun upgrade --canary
# 升级到特定版本
bun upgrade bun@1.2.0
bun init #
初始化项目 #
bash
# 交互式初始化
bun init
# 指定项目类型
bun init --help
初始化选项:
text
? What kind of project is this? (Use arrow keys)
❯ blank - A minimal project
react - A React project
next - A Next.js project
lib - A library project
bun completions #
生成补全脚本 #
bash
# Bash
bun completions bash > ~/.local/share/bash-completion/completions/bun
# Zsh
bun completions zsh > ~/.zsh/completion/_bun
# Fish
bun completions fish > ~/.config/fish/completions/bun.fish
# PowerShell
bun completions powershell > $PROFILE
bun pm #
包管理工具 #
bash
# 查看缓存位置
bun pm cache
# 清理缓存
bun pm cache rm
# 查看 bin 目录
bun pm bin
# 查看全局 bin 目录
bun pm bin -g
# 查看依赖树
bun pm ls
# 查看过时的依赖
bun pm outdated
# 查看 package.json 中的脚本
bun pm scripts
bun repl #
交互式环境 #
bash
# 启动 REPL
bun repl
# 在 REPL 中
> const name = "Bun"
> console.log(name)
Bun
> .exit # 退出
REPL 快捷键 #
| 快捷键 | 功能 |
|---|---|
.help |
显示帮助 |
.exit |
退出 REPL |
.clear |
清除上下文 |
.load |
加载文件 |
.save |
保存会话 |
| Tab | 自动补全 |
| ↑/↓ | 历史命令 |
环境变量 #
常用环境变量 #
bash
# Bun 安装目录
BUN_INSTALL=/path/to/bun
# 缓存目录
BUN_CACHE_DIR=/path/to/cache
# 禁用缓存
BUN_DISABLE_CACHE=1
# HTTP 代理
HTTP_PROXY=http://proxy:8080
HTTPS_PROXY=http://proxy:8080
# Node 兼容模式
BUN_NO_NODE_COMPAT=1
# 颜色输出
NO_COLOR=1
FORCE_COLOR=1
使用 .env 文件 #
bash
# .env
DATABASE_URL=postgres://localhost:5432/mydb
API_KEY=secret123
DEBUG=true
typescript
// 自动加载 .env
console.log(process.env.DATABASE_URL);
console.log(Bun.env.API_KEY);
bash
# 指定 env 文件
bun --env-file=.env.production run index.ts
# 使用多个 env 文件
bun --env-file=.env.local --env-file=.env run index.ts
命令速查表 #
| 命令 | 说明 |
|---|---|
bun run <file> |
运行文件 |
bun <file> |
运行文件(简写) |
bun install |
安装依赖 |
bun add <pkg> |
添加依赖 |
bun remove <pkg> |
移除依赖 |
bun update |
更新依赖 |
bun test |
运行测试 |
bun build |
打包代码 |
bunx <cmd> |
执行包命令 |
bun create |
创建项目 |
bun upgrade |
升级 Bun |
bun init |
初始化项目 |
bun repl |
交互式环境 |
bun pm |
包管理工具 |
下一步 #
现在你已经掌握了 Bun CLI 的基本用法,接下来学习 运行时特性 深入了解 Bun 的运行时 API。
最后更新:2026-03-29