When to Use
User needs Bun expertise — fast JavaScript/TypeScript runtime, bundler, and package manager. Agent handles migration from Node, bundling for web/server, and troubleshooting compatibility issues.
Quick Reference
| Topic | File |
|---|
| Node.js API differences | INLINECODE0 |
| Bundler configuration |
bundler.md |
| Package management |
packages.md |
Runtime Compatibility Traps
- -
process.nextTick timing differs from Node — race conditions appear that didn't exist before, use queueMicrotask for cross-runtime code - INLINECODE5 and
__filename don't exist in ESM — use import.meta.dir and import.meta.file, forgetting causes ReferenceError - INLINECODE9 misses events that Node catches — file watcher scripts silently miss changes, add polling fallback
- INLINECODE10 options subset — some stdio configurations silently ignored, test subprocess code explicitly
- INLINECODE11 module not supported — app crashes immediately if code uses cluster, must refactor to workers
- INLINECODE12 module partial — sandboxed code may escape or behave differently, security implications
Bundler Traps
- -
--target=browser strips Node APIs silently — build succeeds, then runtime crashes on fs, path, etc. - INLINECODE16 requires
--format=esm — error message doesn't mention this, just fails cryptically - Everything bundled by default — server code bundles node_modules, use
--external:package for server deps - Tree-shaking assumes no side effects — code with side effects may be removed, add
"sideEffects": false to package.json or lose code - CSS imports work differently than webpack —
url() paths resolve wrong, test in actual browser - INLINECODE21 mangles names aggressively — debugging production crashes is harder, use
--minify-syntax for safer minification
Package Manager Traps
- -
bun.lockb is binary format — can't diff, can't merge, Git conflicts require delete and regenerate - Peer dependencies auto-installed unlike npm — version conflicts appear silently, different versions than npm would pick
- INLINECODE24 resolves differently than npm — "works on my machine" when teammate uses npm
- Workspaces
link: protocol behaves differently — imports from workspace packages may fail - INLINECODE26 modifies
package.json formatting — unwanted diff noise in commits - No
npm audit equivalent — security vulnerabilities not surfaced automatically
TypeScript Traps
- - Bun runs TypeScript directly without
tsc — type errors don't stop execution, bugs ship to production - Type-only imports may be kept — bundle size larger than expected
- INLINECODE30 paths work differently — imports that worked in Node+tsc may fail
- Decorators experimental — behavior may differ from tsc, especially with legacy decorators
Testing Traps
- -
bun test has different assertion API — tests written for Jest need adaptation - Mock timing differs — tests that pass in Jest may fail or flake
- No native coverage like c8/nyc — need different tooling
- Snapshot format incompatible with Jest — can't share snapshots between runners
Hot Reload Traps
- -
bun --hot doesn't reload native modules — changes require restart - State preserved across reloads — bugs from stale state hard to debug
- WebSocket connections not re-established — clients appear connected but dead
何时使用
用户需要Bun的专业知识——快速JavaScript/TypeScript运行时、打包器和包管理器。智能体负责处理从Node迁移、为Web/服务器打包,以及排查兼容性问题。
快速参考
| 主题 | 文件 |
|---|
| Node.js API差异 | node-compat.md |
| 打包器配置 |
bundler.md |
| 包管理 | packages.md |
运行时兼容性陷阱
- - process.nextTick时序与Node不同——会出现之前不存在的竞态条件,跨运行时代码请使用queueMicrotask
- dirname和filename在ESM中不存在——请使用import.meta.dir和import.meta.file,忘记会导致ReferenceError
- fs.watch会遗漏Node能捕获的事件——文件监视脚本静默错过变更,需添加轮询回退
- child_process.spawn选项子集——某些stdio配置被静默忽略,需显式测试子进程代码
- 不支持cluster模块——使用cluster的代码会立即崩溃,必须重构为workers
- vm模块不完整——沙箱代码可能逃逸或行为异常,存在安全隐患
打包器陷阱
- - --target=browser静默移除Node API——构建成功,但运行时在fs、path等处崩溃
- --splitting需要--format=esm——错误信息未提及此要求,仅神秘失败
- 默认打包所有内容——服务器代码会打包node_modules,对服务器依赖请使用--external:package
- 摇树优化假设无副作用——有副作用的代码可能被移除,需在package.json中添加sideEffects: false,否则代码会丢失
- CSS导入行为与webpack不同——url()路径解析错误,需在真实浏览器中测试
- --minify激进地混淆名称——调试生产崩溃更困难,使用--minify-syntax进行更安全的压缩
包管理器陷阱
- - bun.lockb是二进制格式——无法diff,无法合并,Git冲突需删除后重新生成
- 对等依赖自动安装(与npm不同)——版本冲突静默出现,版本可能与npm选择的不同
- bun install解析方式与npm不同——队友使用npm时会出现在我机器上能运行的问题
- 工作区link:协议行为不同——从工作区包导入可能失败
- bun add修改package.json格式——提交中产生不必要的diff噪音
- 没有npm audit的等效功能——安全漏洞不会自动暴露
TypeScript陷阱
- - Bun直接运行TypeScript无需tsc——类型错误不会阻止执行,bug会发布到生产环境
- 仅类型导入可能被保留——打包体积超出预期
- tsconfig.json路径行为不同——在Node+tsc中能工作的导入可能失败
- 装饰器为实验性——行为可能与tsc不同,尤其是传统装饰器
测试陷阱
- - bun test有不同断言API——为Jest编写的测试需要适配
- Mock时序不同——在Jest中通过的测试可能失败或不稳定
- 没有类似c8/nyc的原生覆盖率——需要不同的工具
- 快照格式与Jest不兼容——无法在运行器之间共享快照
热重载陷阱
- - bun --hot不重载原生模块——变更需要重启
- 跨重载保持状态——陈旧状态导致的bug难以调试
- WebSocket连接不会重新建立——客户端看似已连接但实际已失效