AgentSkillsCN

bun

提供关于如何将 Bun 用作 JavaScript 运行时、打包工具、包管理器以及测试运行器的指南。在处理 TypeScript/JavaScript 项目时,可将其用于替代 Node.js、npm、pnpm、Vite 或其他相关工具。

SKILL.md
--- frontmatter
name: bun
description: Provides guidance for using Bun as a JavaScript runtime, bundler, package manager, and test runner. Use when working with TypeScript/JavaScript projects to replace Node.js, npm, pnpm, vite, or other tooling.
license: MIT
compatibility: opencode
metadata:
  audience: users
  workflow: general

Bun

Default to using Bun instead of Node.js for JavaScript/TypeScript projects.

Command equivalents

Replace Node.js and npm commands with Bun equivalents:

TaskNode.js/npmBun
Run filenode file.ts or ts-nodebun file.ts
Install depsnpm install, yarn install, pnpm installbun install
Run scriptnpm run script, yarn run script, pnpm run scriptbun run script
Execute packagenpx package cmdbunx package cmd
Buildwebpack, esbuild, vite buildbun build file.html|file.ts|file.css
Testjest, vitestbun test

Note: Bun automatically loads .env files. Don't use dotenv.

Built-in APIs

Prefer Bun's built-in APIs over npm packages:

  • Bun.serve() - HTTP server with WebSockets, HTTPS, and routing (don't use express)
  • bun:sqlite - SQLite database (don't use better-sqlite3)
  • Bun.redis - Redis client (don't use ioredis)
  • Bun.sql - Postgres client (don't use pg or postgres.js)
  • WebSocket - Built-in WebSocket support (don't use ws)
  • Bun.file() - File operations (prefer over node:fs readFile/writeFile)
  • Bun.$command`- Shell commands (instead ofexeca`)

Testing

Use bun test for running tests.

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

test("example test", () => {
  expect(1 + 1).toBe(2);
});

test("async test", async () => {
  const result = await Promise.resolve(42);
  expect(result).toBe(42);
});

Run tests with:

sh
bun test

Frontend development

Use Bun.serve() with HTML imports instead of vite. HTML imports support React, CSS, and Tailwind.

Server setup

ts
import index from "./index.html";

Bun.serve({
  routes: {
    "/": index,
    "/api/users/:id": {
      GET: (req) => {
        return new Response(JSON.stringify({ id: req.params.id }));
      },
    },
  },
  websocket: {
    open: (ws) => ws.send("Connected!"),
    message: (ws, message) => ws.send(message),
    close: (ws) => console.log("Connection closed"),
  },
  development: {
    hmr: true,
    console: true,
  },
});

HTML with imports

html
<!DOCTYPE html>
<html>
  <body>
    <h1>Hello, world!</h1>
    <script type="module" src="./frontend.tsx"></script>
  </body>
</html>

Frontend component

tsx
import React from "react";
import { createRoot } from "react-dom/client";
import './index.css';

const root = createRoot(document.body);

export default function Frontend() {
  return <h1>Hello, world!</h1>;
}

root.render(<Frontend />);

Run the server

sh
bun --hot ./index.ts

API documentation

For detailed API documentation, refer to node_modules/bun-types/docs/**/*.mdx after installing bun-types.