bun
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBun Runtime
Bun 运行时
Use Bun as the default JavaScript/TypeScript runtime and package manager.
将Bun作为默认的JavaScript/TypeScript运行时和包管理器。
Command Mappings
命令映射
| Instead of | Use |
|---|---|
| |
| |
| |
| |
| |
| |
Bun automatically loads files - don't use dotenv.
.env| 替代工具 | 使用Bun命令 |
|---|---|
| |
| |
| |
| |
| |
| |
Bun会自动加载文件 - 无需使用dotenv。
.envBun-Specific APIs
Bun专属API
Prefer these over Node.js equivalents:
| API | Purpose | Don't use |
|---|---|---|
| HTTP server with WebSocket, HTTPS, routes | express |
| SQLite database | better-sqlite3 |
| Redis client | ioredis |
| Postgres client | pg, postgres.js |
| File operations | node:fs readFile/writeFile |
| Shell commands | execa |
| WebSocket client (built-in) | ws |
优先使用这些API而非Node.js的等效替代:
| API | 用途 | 不推荐使用 |
|---|---|---|
| 支持WebSocket、HTTPS和路由的HTTP服务器 | express |
| SQLite数据库 | better-sqlite3 |
| Redis客户端 | ioredis |
| Postgres客户端 | pg, postgres.js |
| 文件操作 | node:fs readFile/writeFile |
| Shell命令 | execa |
| 内置WebSocket客户端 | ws |
Testing
测试
Use for tests:
bun:testts
import { test, expect } from "bun:test";
test("description", () => {
expect(1).toBe(1);
});Run with .
bun test使用进行测试:
bun:testts
import { test, expect } from "bun:test";
test("description", () => {
expect(1).toBe(1);
});执行运行测试。
bun testFrontend Development
前端开发
Use HTML imports with instead of Vite. Supports React, CSS, Tailwind.
Bun.serve()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 , , automatically. CSS is bundled via tags.
.tsx.jsx.js<link>Run with for HMR.
bun --hot ./server.ts使用结合HTML导入替代Vite。支持React、CSS、Tailwind。
Bun.serve()服务器端:
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的打包器会自动转译、、文件。CSS通过标签进行打包。
.tsx.jsx.js<link>执行以启用热模块替换(HMR)。
bun --hot ./server.tsDocumentation
文档
For detailed API docs, see .
node_modules/bun-types/docs/**.md如需查看详细的API文档,请访问。
node_modules/bun-types/docs/**.md