agentleFS
belos-streetbelos-street/grimoire/skills/bun/skill.md

bun

Skill8 starsChanged 3 months agoMITView on GitHub

Fast, modern JavaScript runtime and toolkit. IMPORTANT: This is Bun, NOT Node.js. Use Bun-specific APIs below. Do NOT use Node.js APIs like require(), process, fs (use Bun.file() instead).

  • Pipes a download into a shell
  • Reads credentials
  • Installs packages

18 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use analytics-metrics, aws-account-management and 9 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • hoodiniSkill

    analytics-metrics

    Build data visualization and analytics dashboards. Use when creating charts, KPI displays, metrics dashboards, or data visualization components. Triggers on analytics, dashboard, charts, metrics, KPI, data visualization, Recharts.

    Also in 3 projects that use this

  • hoodiniSkill

    aws-account-management

    Manage AWS accounts, organizations, IAM, and billing. Use when setting up AWS Organizations, managing IAM policies, controlling costs, or implementing multi-account strategies. Triggers on AWS Organizations, AWS IAM, AWS billing, Cost Explorer, SCPs, multi-account, AWS SSO, Identity Center.

    Also in 3 projects that use this

  • kelvincushmanSkill

    gh-commit

    Create well-formatted commits with conventional commit format.

    Also in 3 projects that use this

  • kelvincushmanSkill

    prek

    Use when setting up or running hooks with prek in any repository. prek is a Rust drop-in alternative to pre-commit for running Git hooks that check, format, lint, and validate code and repository files. Prefer it when speed, workspace mode, built-in hooks, shared toolchains, or native TOML configuration matter.

    Also in 3 projects that use this

  • ant-designSkill

    gh-create-pr

    使用 GitHub CLI (gh) 推送当前分支并创建或更新 PR。在用户说「提 PR」「新建 PR」「用 gh 建 PR」「submit PR」「create pull request」时使用。

    Also in 3 projects that use this

  • +6 more from hoodini

The file

Files
skill.md

Bun 是面向现代 JavaScript 生态的全新运行时,定位为 Node.js 的替代方案,基于 JavaScriptCore 引擎和 Zig 语言编写。请使用 Bun 特有的 API,不要使用 Node.js 的写法。

Quick Start

# Install Bun
curl -fsSL https://bun.sh/install | bash

# Run a file
bun run index.ts

# Install dependencies
bun install

# Run tests
bun test

Key Features

  • Native TypeScript: 直接运行 .ts/.tsx 文件,无需转译
  • Built-in Tools: 包管理器、测试、打包工具内置
  • Node.js Compatible: 兼容大部分 Node.js 生态
  • Web APIs: 原生 fetch, WebSocket, ReadableStream
  • Bun.serve(): 高性能 HTTP 服务
  • Bun.file(): 简化文件操作
  • Bun.password: 原生密码哈希(argon2)
  • bun:test: 零配置测试框架

Module Import/Export

重要: Bun 默认使用 ES 模块,兼容 CommonJS,无需额外配置。

// ✅ Bun 写法 - 默认 ES 模块
import fs from 'fs/promises';
export const foo = 'bar';

// ✅ Bun 写法 - 原生支持 TS/JSX(无需转译)
import App from './App.tsx';

// ✅ Bun 兼容 CommonJS(无需额外配置)
const fs = require('fs');
module.exports = { foo: 'bar' };

// ❌ 避免 - Node.js CommonJS 写法
const fs = require('fs');  // 可以用,但推荐上面写法

Global Objects

重要: Bun 有自己的全局对象,优先使用 Bun 特有的写法。

// ✅ Bun 特有 - import.meta.dir/file (所有模块类型通用)
console.log(import.meta.dir);  // 当前目录
console.log(import.meta.file); // 当前文件

// ❌ 避免 - Node.js 写法
// console.log(__dirname);  // 不推荐
// console.log(__filename); // 不推荐

// ✅ Bun 特有 - Bun 全局对象
console.log(Bun.env.NODE_ENV);       // 自动读取 .env
console.log(Bun.version);            // Bun 版本
console.log(Bun.memoryUsage());      // 内存使用

// ✅ Bun 兼容 - process 仍可用
console.log(process.env.NODE_ENV);
console.log(process.pid);

// ✅ Bun 特有 - crypto 全局可用
const bytes = crypto.randomBytes(32);

// ❌ 避免 - Node.js 显式引入
// const crypto = require('crypto'); // 不需要

HTTP Server (最核心差异)

重要: 使用 Bun.serve() 而非 http.createServer()。

// ✅ Bun 写法 - Bun.serve() (简洁高效)
Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response('Hello Bun', {
      headers: { 'Content-Type': 'text/plain' }
    });
  }
});

// ✅ Bun 特有 - 原生 WebSocket 支持
Bun.serve({
  port: 3000,
  fetch(req) {
    if (req.headers.get('upgrade') === 'websocket') {
      const ws = new WebSocket(req);
      ws.on('message', (data) => console.log('Message:', data));
      return ws;
    }
    return new Response('Hello Bun');
  }
});

// ❌ 避免 - Node.js 写法
// const http = require('http');
// const server = http.createServer((req, res) => { ... });

File Operations

重要: 使用 Bun.file() 和 Bun.write() 而非 fs 模块。

// ✅ Bun 写法 - Bun.file() (简洁高效)
// 读取文件
const file = Bun.file('./test.txt');
const text = await file.text();           // 读取为文本
const json = await file.json();            // 读取为 JSON(Bun 特有)
const buffer = = await file.arrayBuffer(); // 读取为 Buffer

// 写入文件
await Bun.write('./test.txt', 'Hello Bun');
await Bun.write('./test.json', { name: 'Bun' }); // 自动序列化

// ✅ Bun 兼容 - 目录操作仍需 fs
import { readdir, mkdir } from 'node:fs/promises';
const files = await readdir(import.meta.dir);
await mkdir('./newDir', { recursive: true });

// ❌ 避免 - Node.js fs 写法(可以但不是最优)
// const fs = require('fs/promises');
// const content = await fs.readFile('./test.txt', 'utf8');

Cryptography

重要: 使用 Bun.password 和 Bun.hash 而非第三方库。

// ✅ Bun 特有 - 密码哈希(无需第三方包)
const hash = await Bun.password.hash('123456'); // 默认 argon2 算法
const isMatch = await Bun.password.verify('123456', hash);

// ✅ Bun 特有 - 哈希计算
const hash1 = Bun.hash('test');        // 默认 wyhash,返回 32 位整数
const hash2 = Bun.hash.sha256('test'); // SHA256,返回十六进制

// ❌ 避免 - Node.js bcrypt 写法
// const bcrypt = require('bcrypt');
// const hash = await bcrypt.hash('123456', 10);

Environment Variables

重要: Bun 自动读取 .env,使用 Bun.env 而非 dotenv。

// ✅ Bun 特有 - 自动读取 .env(无需依赖)
console.log(Bun.env.DB_URL);
console.log(Bun.env.PORT);

// ✅ Bun 兼容 - process.env 仍可用
console.log(process.env.DB_URL);

// ✅ Bun 特有 - TypeScript 类型声明
declare module "bun" {
  interface Env {
    DB_URL: string;
    PORT: string;
  }
}
Bun.env.DB_URL; // 自动提示类型

// ❌ 避免 - Node.js dotenv 写法
// require('dotenv').config(); // 不需要

Package Manager

# ✅ Bun 写法
bun install axios
bun run start
bunx cowsay "Hello"

# ❌ 避免 - npm 写法(可以用但推荐 bun)
# npm install axios
# npm run start

Testing

重要: 使用 bun:test 而非 Jest/Vitest。

// ✅ Bun 写法 - bun:test(零配置,速度快)
import { test, expect } from 'bun:test';

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

// ❌ 避免 - Jest 写法(不需要安装)
// const { test, expect } = require('@jest/globals');

Common Patterns

Running TypeScript

// ✅ Bun 直接运行(无需配置 tsc)
// app.ts
interface User {
  id: number;
  name: string;
}

const user: User = { id: 1, name: 'John' };
console.log(user);
bun run app.ts

SQLite

const db = new Bun.Database('my.db');
const users = db.query('SELECT * FROM users').all();

Migration Checklist

Node.jsBunNotes
require()import默认 ESM
__dirnameimport.meta.dir
fs.readFileBun.file().text()更简洁
http.createServerBun.serve()性能更好
bcryptBun.password内置
dotenvBun.env自动读取
Jestbun:test零配置
npmbun速度更快

Key Points

  1. 使用 Bun API: 优先使用 Bun.serve(), Bun.file(), Bun.write(), Bun.password, Bun.hash
  2. 使用 import: 默认 ES 模块,直接 import 而非 require
  3. 使用 import.meta.dir: 获取当前目录
  4. 使用 Bun.env: 访问环境变量,自动读取 .env
  5. 使用 bun:test: 内置测试框架
  6. 使用 bun: 包管理和运行脚本

Resources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.