bun-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBun CLI
Bun CLI
Bun is an all-in-one JavaScript/TypeScript runtime, package manager, bundler, and test runner. Bun runs TypeScript natively — directly, no compile step, no , no . Always use instead of , , , , or in Bun projects.
bun file.tstscts-nodebunnodenpmnpxyarnpnpmVerified against Bun v1.3.14 (2026-05-28).
Bun 是一款一体化的 JavaScript/TypeScript 运行时、包管理器、打包工具和测试运行器。Bun 原生支持 TypeScript——直接运行 ,无需编译步骤,无需 ,无需 。在 Bun 项目中,请始终使用 替代 、、、 或 。
bun file.tstscts-nodebunnodenpmnpxyarnpnpm已针对 Bun v1.3.14 验证(2026-05-28)。
Detecting Bun Projects
识别 Bun 项目
A project uses Bun if any of these are present:
- or
bun.lockin the project rootbun.lockb - in the project root
bunfig.toml - field in
bun(e.g.,package.json)"bun": { "install": { ... } } - Package manager field:
"packageManager": "bun@..." - in
[run] bun = true(forces Bun runtime for all scripts)bunfig.toml
如果项目中存在以下任一内容,则表明该项目使用 Bun:
- 项目根目录下的 或
bun.lockbun.lockb - 项目根目录下的
bunfig.toml - 中的
package.json字段(例如:bun)"bun": { "install": { ... } } - 包管理器字段:
"packageManager": "bun@..." - 中的
bunfig.toml(强制所有脚本使用 Bun 运行时)[run] bun = true
Critical Rule
关键规则
In a Bun project, ALWAYS use for everything. Never fall back to , , , , or . This avoids compatibility issues, unnecessary retries, and cryptic errors from Node.js/npm not understanding Bun-specific features (workspace protocol, lockfile format, trustedDependencies, etc.).
bunnodenpmnpxyarnpnpm- Run files: (not
bun file.ts)node file.ts - Run scripts: (not
bun run dev)npm run dev - Execute binaries: (not
bunx tool)npx tool - Install packages: (not
bun add pkg)npm install pkg - Run tests: (not
bun testornpx jest)node --test
在 Bun 项目中,所有操作都必须使用 。永远不要回退使用 、、、 或 。这可以避免兼容性问题、不必要的重试,以及 Node.js/npm 无法理解 Bun 特定功能(工作区协议、锁文件格式、trustedDependencies 等)导致的晦涩错误。
bunnodenpmnpxyarnpnpm- 运行文件:(而非
bun file.ts)node file.ts - 运行脚本:(而非
bun run dev)npm run dev - 执行二进制文件:(而非
bunx tool)npx tool - 安装包:(而非
bun add pkg)npm install pkg - 运行测试:(而非
bun test或npx jest)node --test
Read-Only Commands (safe, no side effects)
只读命令(安全,无副作用)
| Command | Purpose |
|---|---|
| Runtime version |
| Package metadata, available versions |
| List all published versions |
| List installed packages |
| List all (including transitive) |
| Print lockfile hash |
| Show cache directory |
| Check for outdated dependencies |
| Security vulnerability audit |
| Run test suite |
| Run linter (project-specific) |
| Type checking (project-specific) |
Reference: Seefor copy-pastereferences/allowlist.mdpatterns for Claude Code / OpenCode settings.Bash(command:*)
| 命令 | 用途 |
|---|---|
| 查看运行时版本 |
| 查看包元数据、可用版本 |
| 列出所有已发布版本 |
| 列出已安装的包 |
| 列出所有包(包括间接依赖) |
| 打印锁文件哈希值 |
| 显示缓存目录 |
| 检查过时依赖 |
| 安全漏洞审计 |
| 运行测试套件 |
| 运行代码检查工具(项目特定) |
| 类型检查(项目特定) |
参考:查看获取可复制粘贴到 Claude Code / OpenCode 设置中的references/allowlist.md模式。Bash(command:*)
npm/npx/node to Bun Translation
npm/npx/node 到 Bun 的命令映射
| npm/npx/node | Bun equivalent |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| npm/npx/node | Bun 等效命令 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Key Behavioral Differences
核心行为差异
- No prefix needed:
npm runworks, but so doesbun run dev(direct script execution)bun dev - flag: Forces Bun runtime instead of Node.js for scripts that use
--bunin their shebang. Innode, setbunfig.tomlto make this the default[run] bun = true - Lockfile: Bun uses (text-based, v1.2+) or
bun.lock(binary, legacy). Text lockfile is default for new projectsbun.lockb - Workspace commands: Use flag:
--filterbun --filter 'pkg-name' add dep - Lifecycle scripts: Bun ignores lifecycle scripts by default for security. Use in package.json to allowlist packages that need postinstall etc.
trustedDependencies
- 无需 前缀:
npm run可用,bun run dev也可直接执行脚本bun dev - 标志:对于 shebang 中使用
--bun的脚本,强制使用 Bun 运行时。在node中设置bunfig.toml可将其设为默认值[run] bun = true - 锁文件:Bun 使用 (文本格式,v1.2+)或
bun.lock(二进制格式,旧版)。新项目默认使用文本锁文件bun.lockb - 工作区命令:使用 标志:
--filterbun --filter 'pkg-name' add dep - 生命周期脚本:出于安全考虑,Bun 默认忽略生命周期脚本。在 package.json 中使用 来允许需要 postinstall 等脚本的包
trustedDependencies
Package Management
包管理
Installing Dependencies
安装依赖
bash
bun install # Install all from package.json
bun install --frozen-lockfile # CI mode: fail if lockfile needs update
bun install --no-save # Install without updating package.json
bun install --production # Skip devDependencies
bun install --dry-run # Show what would be installedbash
bun install # 从 package.json 安装所有依赖
bun install --frozen-lockfile # CI 模式:如果锁文件需要更新则失败
bun install --no-save # 安装但不更新 package.json
bun install --production # 跳过 devDependencies
bun install --dry-run # 显示将要安装的内容Adding/Removing Packages
添加/移除包
bash
bun add pkg # Add to dependencies
bun add pkg@version # Add specific version
bun add -d pkg # Add to devDependencies (--dev)
bun add -D pkg # Same as -d
bun add --optional pkg # Add to optionalDependencies
bun add -g pkg # Install globally
bun add --exact pkg # Pin exact version (no ^)
bun remove pkg # Remove packagebash
bun add pkg # 添加到 dependencies
bun add pkg@version # 添加特定版本
bun add -d pkg # 添加到 devDependencies(--dev)
bun add -D pkg # 与 -d 效果相同
bun add --optional pkg # 添加到 optionalDependencies
bun add -g pkg # 全局安装
bun add --exact pkg # 固定精确版本(无 ^)
bun remove pkg # 移除包Updating and Inspecting
更新与检查
bash
bun update # Update all packages
bun update pkg # Update specific package
bun outdated # Show outdated packages
bun info pkg # Show package metadata
bun info pkg versions # List all available versions
bun pm ls # List installed packages
bun pm ls --all # List all (including transitive)
bun pm hash # Print lockfile hash
bun pm cache # Show cache directory
bun pm cache rm # Clear cachebash
bun update # 更新所有包
bun update pkg # 更新特定包
bun outdated # 显示过时的包
bun info pkg # 显示包元数据
bun info pkg versions # 列出所有可用版本
bun pm ls # 列出已安装的包
bun pm ls --all # 列出所有包(包括间接依赖)
bun pm hash # 打印锁文件哈希值
bun pm cache # 显示缓存目录
bun pm cache rm # 清除缓存Linking and Patching
链接与补丁
bash
bun link # Register current package as linkable
bun link pkg-name # Link a registered package
bun pm pack # Create tarball of package
bun patch pkg # Start patching a package
bun patch --commit pkg-dir # Apply patchbash
bun link # 将当前包注册为可链接包
bun link pkg-name # 链接已注册的包
bun pm pack # 创建包的 tarball 文件
bun patch pkg # 开始给包打补丁
bun patch --commit pkg-dir # 应用补丁Publishing
发布
bash
bun publish # Publish to npm
bun publish --dry-run # Preview what would be published
bun publish --tag beta # Publish with tag
bun publish --access public # Set access levelReference: Seefor complete flag details.references/package-management.md
bash
bun publish # 发布到 npm
bun publish --dry-run # 预览将要发布的内容
bun publish --tag beta # 带标签发布
bun publish --access public # 设置访问级别参考:查看获取完整的标志详情。references/package-management.md
Running Scripts and Files
运行脚本与文件
Direct Execution
直接执行
bash
bun file.ts # Run TypeScript/JavaScript directly
bun run script-name # Run package.json script
bun script-name # Short form (if no conflict with bun commands)
bun --watch file.ts # Re-run on file changes
bun --hot file.ts # Hot reload (preserves state)
bun --env-file .env file.ts # Load env file
bun --env-file .env.local --env-file .env file.ts # Multiple env filesbash
bun file.ts # 直接运行 TypeScript/JavaScript 文件
bun run script-name # 运行 package.json 中的脚本
bun script-name # 简写形式(如果与 bun 命令无冲突)
bun --watch file.ts # 文件变化时重新运行
bun --hot file.ts # 热重载(保留状态)
bun --env-file .env file.ts # 加载环境变量文件
bun --env-file .env.local --env-file .env file.ts # 加载多个环境变量文件bunx (npx Replacement)
bunx(替代 npx)
bash
bunx command # Run package binary (auto-installs if needed)
bunx --bun command # Force Bun runtime for the command
bunx command@version # Run specific versionbash
bunx command # 运行包的二进制文件(需要时自动安装)
bunx --bun command # 强制使用 Bun 运行时执行命令
bunx command@version # 运行特定版本的命令Parallel and Sequential Execution
并行与串行执行
bash
bun --parallel run build lint typecheck # Run all concurrently
bun --sequential run clean build deploy # Run one after anotherbash
bun --parallel run build lint typecheck # 并发运行所有命令
bun --sequential run clean build deploy # 按顺序逐个运行Workspace-Aware Execution
工作区感知执行
bash
bun --filter 'pkg-name' run script # Run in specific workspace
bun --filter '*' run script # Run in all workspaces
bun --filter './apps/*' run build # Run with glob patternbash
bun --filter 'pkg-name' run script # 在特定工作区运行脚本
bun --filter '*' run script # 在所有工作区运行脚本
bun --filter './apps/*' run build # 使用 glob 匹配模式运行Script Flags
脚本标志
bash
bun run --smol file.ts # Reduce memory usage (sacrifice throughput)
bun run --silent script # Suppress script name echo
bun run --shell=bun script # Use Bun's built-in shell (cross-platform, default on Windows)
bun run --shell=system script # Use system shell (default on macOS/Linux)bash
bun run --smol file.ts # 减少内存使用(牺牲吞吐量)
bun run --silent script # 抑制脚本名称回显
bun run --shell=bun script # 使用 Bun 内置 shell(跨平台,Windows 默认)
bun run --shell=system script # 使用系统 shell(macOS/Linux 默认)Zero-Config Frontend Development
零配置前端开发
Run HTML files directly as a dev server -- no Vite, Webpack, or any config needed:
bash
bun ./index.html # Start dev server, auto-bundles JS/TS/CSS
bun --hot ./index.html # With hot module replacementBun automatically transpiles TypeScript, JSX, TSX, and CSS linked from the HTML. Resolves imports in tags. Enables HMR and React Fast Refresh.
node_modules<script>Reference: Seefor complete details.references/running-and-execution.md
直接运行 HTML 文件作为开发服务器——无需 Vite、Webpack 或任何配置:
bash
bun ./index.html # 启动开发服务器,自动打包 JS/TS/CSS
bun --hot ./index.html # 启用热模块替换Bun 会自动转译 HTML 中链接的 TypeScript、JSX、TSX 和 CSS。解析 标签中的 导入。支持 HMR 和 React Fast Refresh。
<script>node_modules参考:查看获取完整详情。references/running-and-execution.md
Testing
测试
Bun includes a built-in test runner compatible with Jest-like syntax.
Bun 内置了兼容 Jest 语法的测试运行器。
Running Tests
运行测试
bash
bun test # Run all test files
bun test file.test.ts # Run specific file
bun test --filter "pattern" # Filter by test name
bun test --timeout 10000 # Set timeout (ms)
bun test --bail # Stop on first failure
bun test --bail 5 # Stop after 5 failures
bun test --rerun-each 3 # Run each test 3 times
bun test --only # Run only tests marked with .only
bun test --todo # Include .todo testsbash
bun test # 运行所有测试文件
bun test file.test.ts # 运行特定文件Coverage
覆盖率
bash
bun test --coverage # Enable code coverage
bun test --coverage-reporter text # Coverage format: text, lcov, json
bun test --coverage-dir ./cov # Output directorybash
bun test --coverage # 启用代码覆盖率
bun test --coverage-reporter text # 覆盖率格式:text、lcov、json
bun test --coverage-dir ./cov # 输出目录Test File Patterns
测试文件模式
By default, Bun finds files matching: , , , , and files in directories.
*.test.{ts,tsx,js,jsx}*_test.{ts,tsx,js,jsx}*.spec.{ts,tsx,js,jsx}*_spec.{ts,tsx,js,jsx}__tests__/默认情况下,Bun 会匹配以下文件:、、、,以及 目录下的文件。
*.test.{ts,tsx,js,jsx}*_test.{ts,tsx,js,jsx}*.spec.{ts,tsx,js,jsx}*_spec.{ts,tsx,js,jsx}__tests__/Snapshot Testing
快照测试
bash
bun test --update-snapshots # Update snapshot filesbash
bun test --update-snapshots # 更新快照文件Watch Mode
监听模式
bash
bun test --watch # Re-run on file changesReference: Seefor test API, mocking, lifecycle hooks, and coverage config.references/testing.md
bash
bun test --watch # 文件变化时重新运行测试参考:查看获取测试 API、模拟、生命周期钩子和覆盖率配置的详情。references/testing.md
Bundling and Compilation
打包与编译
Bundling
打包
bash
bun build ./src/index.ts --outdir ./dist # Bundle to directory
bun build ./src/index.ts --outfile ./dist/out.js # Bundle to single file
bun build ./src/index.ts --target browser # Target: browser (default), bun, node
bun build ./src/index.ts --format esm # Format: esm (default), cjs, iife
bun build ./src/index.ts --minify # Minify output
bun build ./src/index.ts --sourcemap external # Sourcemaps: external, inline, linked, none
bun build ./src/index.ts --splitting # Code splitting (ESM only)bash
bun build ./src/index.ts --outdir ./dist # 打包到目录
bun build ./src/index.ts --outfile ./dist/out.js # 打包为单个文件
bun build ./src/index.ts --target browser # 目标环境:browser(默认)、bun、node
bun build ./src/index.ts --format esm # 格式:esm(默认)、cjs、iife
bun build ./src/index.ts --minify # 压缩输出Standalone Executables
独立可执行文件
bash
bun build ./src/cli.ts --compile # Create self-contained executable
bun build ./src/cli.ts --compile --target bun-linux-x64 # Cross-compile
bun build ./src/cli.ts --compile --minify # Minified executableAvailable compilation targets: , , , , .
bun-linux-x64bun-linux-arm64bun-darwin-x64bun-darwin-arm64bun-windows-x64Browser target (v1.3.10+) -- compile to a self-contained HTML file:
bash
bun build --compile --target=browser ./app.tsx --outfile ./dist/app.htmlbash
bun build ./src/cli.ts --compile # 创建自包含的可执行文件
bun build ./src/cli.ts --compile --target bun-linux-x64 # 交叉编译
bun build ./src/cli.ts --compile --minify # 生成压缩后的可执行文件可用的编译目标:、、、、。
bun-linux-x64bun-linux-arm64bun-darwin-x64bun-darwin-arm64bun-windows-x64浏览器目标(v1.3.10+)——编译为自包含的 HTML 文件:
bash
bun build --compile --target=browser ./app.tsx --outfile ./dist/app.htmlBuild Options
构建选项
bash
bun build ... --external pkg # Exclude from bundle
bun build ... --define 'KEY=VALUE' # Define compile-time constants
bun build ... --loader .ext=type # Custom loaders (js, jsx, ts, tsx, json, css, text, file, base64, dataurl, binary)
bun build ... --entry-naming [dir]/[name].[ext] # Output naming pattern
bun build ... --public-path /cdn/ # Public path prefix for assetsReference: Seefor complete options.references/bundling-and-compilation.md
bash
bun build ... --external pkg # 将包排除在打包之外
bun build ... --define 'KEY=VALUE' # 定义编译时常量
bun build ... --loader .ext=type # 自定义加载器(js、jsx、ts、tsx、json、css、text、file、base64、dataurl、binary)参考:查看获取完整选项。references/bundling-and-compilation.md
Project Initialization
项目初始化
bash
bun init # Initialize new project (creates package.json, tsconfig.json, index.ts)
bun create template-name # Create from template
bun create next-app my-app # Example: create Next.js appbash
bun init # 初始化新项目(创建 package.json、tsconfig.json、index.ts)
bun create template-name # 从模板创建项目
bun create next-app my-app # 示例:创建 Next.js 应用Configuration (bunfig.toml)
配置(bunfig.toml)
Key sections:
toml
[run]
bun = true # Always use Bun runtime (not Node)
[install]
exact = true # Pin exact versions by default
peer = false # Don't auto-install peer deps
production = false # Include devDeps
frozenLockfile = false # Don't fail on lockfile mismatch
globalDir = "~/.bun/install/global" # Global install location
[install.scopes]
"@myorg" = { token = "$NPM_TOKEN", url = "https://npm.pkg.github.com/" }
[test]
coverage = false # Enable coverage by default
coverageReporter = ["text", "lcov"]
timeout = 5000 # Default test timeout
[bundle]
entryPoints = ["./src/index.ts"]
outdir = "./dist"Reference: Seefor complete bunfig.toml reference.references/configuration.md
核心配置段:
toml
[run]
bun = true # 始终使用 Bun 运行时(而非 Node)
[install]
exact = true # 默认固定精确版本
peer = false # 不自动安装 peer 依赖
production = false # 包含 devDependencies
frozenLockfile = false # 锁文件不匹配时不失败
globalDir = "~/.bun/install/global" # 全局安装位置
[install.scopes]
"@myorg" = { token = "$NPM_TOKEN", url = "https://npm.pkg.github.com/" }
[test]
coverage = false # 默认启用覆盖率
coverageReporter = ["text", "lcov"]
timeout = 5000 # 默认测试超时时间
[bundle]
entryPoints = ["./src/index.ts"]
outdir = "./dist"参考:查看获取完整的 bunfig.toml 参考文档。references/configuration.md
Debugging and Profiling
调试与性能分析
bash
bun --inspect file.ts # Start debugger (WebSocket, connect via Chrome DevTools)
bun --inspect-wait file.ts # Wait for debugger to attach before executing
bun --inspect-brk file.ts # Break on first line
bun --cpu-prof file.ts # Generate CPU profile
bun --cpu-prof-md file.ts # CPU profile as Markdown (v1.3.7+)
bun --heap-prof file.ts # Generate heap profile
bun --heap-prof-md file.ts # Heap profile as Markdown (v1.3.7+)
BUN_JSC_logJITCodeForPerf=1 bun file.ts # Linux perf integrationbash
bun --inspect file.ts # 启动调试器(WebSocket,通过 Chrome DevTools 连接)
bun --inspect-wait file.ts # 等待调试器连接后再执行
bun --inspect-brk file.ts # 在第一行断点
bun --cpu-prof file.ts # 生成 CPU 性能分析报告
bun --heap-prof file.ts # 生成堆内存分析报告Environment Variables
环境变量
bash
bun --env-file .env file.ts # Load .env file
bun --env-file .env.local --env-file .env file.ts # Load multiple (left takes precedence)Bun auto-loads , , , by default based on .
.env.env.production.env.local.env.production.localNODE_ENVbash
bun --env-file .env file.ts # 加载 .env 文件
bun --env-file .env.local --env-file .env file.ts # 加载多个文件(左侧文件优先级更高)Bun 会根据 自动加载 、、、。
NODE_ENV.env.env.production.env.local.env.production.localBuilt-in Features That Replace External Tools
替代外部工具的内置功能
Bun has many capabilities built in that eliminate the need for external packages or tooling:
Bun 内置了许多功能,无需依赖外部包或工具:
Native TypeScript
原生 TypeScript 支持
Bun runs , files directly — no , , or needed. The transpiler is built into the runtime. Use to run any TypeScript file immediately.
.ts.tsxtscts-nodetsxbun file.tsBun 可直接运行 、 文件——无需 、 或 。转译器内置在运行时中。使用 即可立即运行任何 TypeScript 文件。
.ts.tsxtscts-nodetsxbun file.tsWorkspace Catalogs
工作区目录
Bun supports protocol in for centralized dependency version management across monorepo workspaces — no need for tools like or :
catalog:package.jsonsyncpackmanypkgjson
// Root package.json
{
"workspaces": ["packages/*"],
"catalog": {
"react": "^19.0.0",
"typescript": "^5.7.0"
}
}
// packages/app/package.json
{
"dependencies": {
"react": "catalog:"
}
}Bun 在 中支持 协议,用于在 monorepo 工作区中集中管理依赖版本——无需 或 等工具:
package.jsoncatalog:syncpackmanypkgjson
// 根目录 package.json
{
"workspaces": ["packages/*"],
"catalog": {
"react": "^19.0.0",
"typescript": "^5.7.0"
}
}
// packages/app/package.json
{
"dependencies": {
"react": "catalog:"
}
}Built-in Test Runner
内置测试运行器
bun testjestvitestmochabun testjestvitestmochaBuilt-in Bundler
内置打包工具
bun buildesbuildwebpackrollupbun buildesbuildwebpackrollupBuilt-in SQLite
内置 SQLite
import { Database } from 'bun:sqlite'better-sqlite3sql.jsimport { Database } from 'bun:sqlite'better-sqlite3sql.jsBuilt-in Shell
内置 Shell
Bun.$execashelljszxBun.$execashelljszxBuilt-in File I/O
内置文件 I/O
Bun.file()Bun.write()fsBun.file()Bun.write()fsBuilt-in Glob
内置 Glob
new Bun.Glob(pattern)globfast-globminimatchnew Bun.Glob(pattern)globfast-globminimatchBuilt-in Password Hashing
内置密码哈希
Bun.password.hash().verify()bcryptargon2Bun.password.hash().verify()bcryptargon2Built-in Compression
内置压缩
Bun.gzipSync()Bun.deflateSync()Bun.zstdCompressSync()zlibBun.gzipSync()Bun.deflateSync()Bun.zstdCompressSync()zlibBuilt-in Semver
内置 Semver
Bun.semver.satisfies().order()semverBun.semver.satisfies().order()semverBuilt-in Runtime APIs
内置运行时 API
For Bun's built-in runtime helpers (, , , , , , , ), see the skill.
Bun.s3Bun.redisBun.ArchiveJSONCJSON5JSONLmarkdowncronbun-api关于 Bun 的内置运行时助手(、、、、、、、),请查看 技能。
Bun.s3Bun.redisBun.ArchiveJSONCJSON5JSONLmarkdowncronbun-apiZero-Config Frontend Dev Server
零配置前端开发服务器
bun ./index.htmlbun ./index.htmlES Decorators
ES 装饰器
TC39 standard ES decorators supported natively (v1.3.10+) — no tsconfig needed.
experimentalDecorators原生支持 TC39 标准 ES 装饰器(v1.3.10+)——无需在 tsconfig 中设置 。
experimentalDecoratorsKey Gotchas
关键注意事项
- Always use not
bun/npm/nodein Bun projectsnpx - Lockfile format: (text, v1.2+) is the default for new projects. Legacy
bun.lockis binary. Don't mix withbun.lockbpackage-lock.json - trustedDependencies: Lifecycle scripts (postinstall, etc.) only run for packages listed in in package.json
trustedDependencies - flag: Some tools (e.g., Next.js) use Node.js by default even when run with
--bun. Usebun runor--bunin[run] bun = trueto force Bun runtimebunfig.toml - Workspace protocol: Use in package.json to reference workspace packages
"workspace:*" - Global binaries: Installed with , located in
bun add -g~/.bun/bin/ - Node.js compatibility: Bun implements most Node.js APIs but some edge cases differ. Check https://bun.sh/docs/runtime/nodejs-apis for compatibility
- TypeScript: Bun runs TypeScript natively with no compilation step. Uses its own transpiler (not tsc)
- Auto-install: Bun can auto-install missing packages on import (disabled by default, enable with in bunfig.toml)
[install] auto = true - vs
bun run:bunruns a package.json script;bun run scriptruns a file directly.bun file.tstries script first, then falls back to filebun script
- 在 Bun 项目中始终使用 ,而非
bun/npm/nodenpx - 锁文件格式:(文本格式,v1.2+)是新项目的默认格式。旧版
bun.lock是二进制格式。不要与bun.lockb混用package-lock.json - trustedDependencies:生命周期脚本(如 postinstall)仅会为 package.json 中 列表里的包运行
trustedDependencies - 标志:某些工具(如 Next.js)即使使用
--bun运行,默认仍使用 Node.js。使用bun run或在--bun中设置bunfig.toml可强制使用 Bun 运行时[run] bun = true - 工作区协议:在 package.json 中使用 引用工作区包
"workspace:*" - 全局二进制文件:使用 安装,位于
bun add -g~/.bun/bin/ - Node.js 兼容性:Bun 实现了大多数 Node.js API,但某些边缘情况存在差异。查看 https://bun.sh/docs/runtime/nodejs-apis 了解兼容性
- TypeScript:Bun 原生运行 TypeScript,无需编译步骤。使用自身的转译器(而非 tsc)
- 自动安装:Bun 可在导入时自动安装缺失的包(默认禁用,可在 bunfig.toml 中设置 启用)
[install] auto = true - vs
bun run:bun运行 package.json 中的脚本;bun run script直接运行文件。bun file.ts会先尝试运行脚本,再回退到运行文件bun script