Scripts2026年4月3日·1 分钟阅读

Bun — All-in-One JavaScript Runtime

Fast JavaScript runtime, bundler, test runner, and package manager in one tool. Drop-in Node.js replacement. 88K+ GitHub stars.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Script
安装
Stage only
信任
信任等级:Established
入口
bun.md
安全暂存命令
npx -y tokrepo@latest install 615b3bf1-5ba3-47bd-99fd-e76dfdf2557d --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

TL;DR
Bun combines JavaScript runtime, bundler, test runner, and package manager in one fast tool. Drop-in Node.js replacement.
§01

What it is

Bun is a JavaScript runtime built from scratch in Zig with JavaScriptCore (Safari's engine) instead of V8. It bundles four tools into one binary: a runtime (replaces Node.js), a bundler (replaces Webpack/esbuild), a test runner (replaces Jest), and a package manager (replaces npm/yarn/pnpm). It supports TypeScript and JSX natively without a compilation step.

Bun targets JavaScript and TypeScript developers who want faster tooling. Its startup time, install speed, and bundling performance are significantly faster than the Node.js ecosystem equivalents.

§02

How it saves time or tokens

Bun's package manager installs dependencies up to 25x faster than npm by using hardlinks and a global cache. The runtime starts in milliseconds. The built-in test runner eliminates Jest configuration. By combining four tools into one, Bun reduces the dependency count and configuration files in your project.

§03

How to use

  1. Install Bun via curl or your package manager.
  2. Use it as a drop-in replacement for Node.js, npm, and Jest.
  3. Run TypeScript files directly without compilation.
# Install Bun
curl -fsSL https://bun.sh/install | bash

# Run a TypeScript file directly
bun run server.ts

# Install packages (faster than npm)
bun install

# Run tests
bun test

# Bundle for production
bun build ./src/index.ts --outdir ./dist
§04

Example

// server.ts - HTTP server with Bun
const server = Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === '/api/hello') {
      return Response.json({ message: 'Hello from Bun' });
    }
    return new Response('Not Found', { status: 404 });
  },
});

console.log(`Listening on ${server.url}`);
§05

Related on TokRepo

§06

Common pitfalls

  • Not all Node.js APIs are implemented in Bun yet. Some npm packages that rely on Node-specific APIs (like child_process edge cases) may not work. Check Bun's compatibility page before migrating.
  • Bun's test runner uses a different assertion API than Jest. Existing Jest test suites may need minor adjustments for expect() matchers.
  • Global installs with bun install -g place binaries in a different path than npm. Make sure Bun's bin directory is in your PATH.

常见问题

Can Bun replace Node.js completely?+

For most projects, yes. Bun implements the majority of Node.js APIs and is compatible with most npm packages. Some edge cases around streams, worker_threads, and native addons may still require Node.js. Check Bun's Node.js compatibility table for your specific dependencies.

How much faster is Bun than Node.js?+

Benchmarks vary by workload. Bun's HTTP server handles more requests per second than Node.js in synthetic benchmarks. Package installs are up to 25x faster. Cold start time is significantly lower. Real-world performance gains depend on your application's bottlenecks.

Does Bun support TypeScript natively?+

Yes. Bun runs TypeScript files directly without a separate compilation step. It transpiles TypeScript on the fly using its built-in transpiler. This eliminates the need for ts-node, tsx, or a build step for TypeScript projects.

Can I use Bun with existing npm packages?+

Yes. Bun reads package.json and node_modules like npm. You can use bun install as a drop-in replacement for npm install. The vast majority of npm packages work with Bun without modification.

Does Bun work on Windows?+

Yes. Bun supports Windows, macOS, and Linux. Windows support was added after the initial launch and is now stable for most use cases. Some platform-specific edge cases may still exist.

引用来源 (3)
🙏

来源与感谢

Created by Oven. Licensed under MIT.

bun — ⭐ 88,700+

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产