bun

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bun Runtime

Bun 运行时

Use Bun as the default JavaScript/TypeScript runtime and package manager.
将Bun作为默认的JavaScript/TypeScript运行时和包管理器。

Command Mappings

命令映射

Instead ofUse
node file.ts
bun file.ts
ts-node file.ts
bun file.ts
npm install
bun install
npm run script
bun run script
jest
/
vitest
bun test
webpack
/
esbuild
bun build
Bun automatically loads
.env
files - don't use dotenv.
替代工具使用Bun命令
node file.ts
bun file.ts
ts-node file.ts
bun file.ts
npm install
bun install
npm run script
bun run script
jest
/
vitest
bun test
webpack
/
esbuild
bun build
Bun会自动加载
.env
文件 - 无需使用dotenv。

Bun-Specific APIs

Bun专属API

Prefer these over Node.js equivalents:
APIPurposeDon't use
Bun.serve()
HTTP server with WebSocket, HTTPS, routesexpress
bun:sqlite
SQLite databasebetter-sqlite3
Bun.redis
Redis clientioredis
Bun.sql
Postgres clientpg, postgres.js
Bun.file()
File operationsnode:fs readFile/writeFile
Bun.$\
cmd``
Shell commandsexeca
WebSocket
WebSocket client (built-in)ws
优先使用这些API而非Node.js的等效替代:
API用途不推荐使用
Bun.serve()
支持WebSocket、HTTPS和路由的HTTP服务器express
bun:sqlite
SQLite数据库better-sqlite3
Bun.redis
Redis客户端ioredis
Bun.sql
Postgres客户端pg, postgres.js
Bun.file()
文件操作node:fs readFile/writeFile
Bun.$\
cmd``
Shell命令execa
WebSocket
内置WebSocket客户端ws

Testing

测试

Use
bun:test
for tests:
ts
import { test, expect } from "bun:test";

test("description", () => {
  expect(1).toBe(1);
});
Run with
bun test
.
使用
bun:test
进行测试:
ts
import { test, expect } from "bun:test";

test("description", () => {
  expect(1).toBe(1);
});
执行
bun test
运行测试。

Frontend Development

前端开发

Use HTML imports with
Bun.serve()
instead of Vite. Supports React, CSS, Tailwind.
Server:
ts
import index from "./index.html"

Bun.serve({
  routes: {
    "/": index,
    "/api/users/:id": {
      GET: (req) => Response.json({ id: req.params.id }),
    },
  },
  development: { hmr: true, console: true }
})
HTML file:
html
<html>
  <body>
    <script type="module" src="./app.tsx"></script>
  </body>
</html>
Bun's bundler transpiles
.tsx
,
.jsx
,
.js
automatically. CSS is bundled via
<link>
tags.
Run with
bun --hot ./server.ts
for HMR.
使用
Bun.serve()
结合HTML导入替代Vite。支持React、CSS、Tailwind。
服务器端:
ts
import index from "./index.html"

Bun.serve({
  routes: {
    "/": index,
    "/api/users/:id": {
      GET: (req) => Response.json({ id: req.params.id }),
    },
  },
  development: { hmr: true, console: true }
})
HTML文件:
html
<html>
  <body>
    <script type="module" src="./app.tsx"></script>
  </body>
</html>
Bun的打包器会自动转译
.tsx
.jsx
.js
文件。CSS通过
<link>
标签进行打包。
执行
bun --hot ./server.ts
以启用热模块替换(HMR)。

Documentation

文档

For detailed API docs, see
node_modules/bun-types/docs/**.md
.
如需查看详细的API文档,请访问
node_modules/bun-types/docs/**.md