bunjs

SKILL.md

Bun.js

All-in-one JavaScript/TypeScript toolkit: runtime, package manager, bundler, test runner.

Quick Reference

# Runtime
bun run index.ts          # Execute file (TS/JSX native)
bun --watch index.ts      # Watch mode
bun --hot index.ts        # Hot reload

# Package Manager
bun install               # Install deps (25x faster than npm)
bun add <pkg>             # Add dependency
bun add -d <pkg>          # Add dev dependency
bun remove <pkg>          # Remove dependency
bun update                # Update deps
bunx <pkg>                # Execute package (like npx)

# Bundler
bun build ./src --outdir ./dist
bun build --minify --sourcemap linked

# Test Runner
bun test                  # Run tests
bun test --watch          # Watch mode
bun test --coverage       # With coverage

Runtime

Execution

bun run script.ts         # Run file
bun run start             # Run package.json script
bun --bun run start       # Force Bun runtime (not Node)

Built-in APIs

// HTTP Server
Bun.serve({
    port: 3000,
    fetch(req) {
        return new Response("Hello!");
    },
});

// File I/O
const file = Bun.file("./data.json");
const text = await file.text();
await Bun.write("out.txt", "content");

// SQLite (native)
import { Database } from "bun:sqlite";
const db = new Database("mydb.sqlite");

// Hashing
const hash = Bun.hash("data");
const password = await Bun.password.hash("secret");

// Shell
import { $ } from "bun";
await $`ls -la`;

Environment Variables

// Auto-loads .env files
const apiKey = Bun.env.API_KEY;
// or
const apiKey = process.env.API_KEY;

Package Manager

Commands

Command Description
bun install Install all deps
bun add <pkg> Add dependency
bun add -d <pkg> Add dev dependency
bun add -g <pkg> Add global
bun remove <pkg> Remove
bun update Update all
bun outdated Check outdated

Lockfile

  • bun.lock - Text lockfile (v1.2+)
  • bun.lockb - Binary lockfile (legacy)

CI/CD

bun ci                    # Frozen lockfile install
bun install --frozen-lockfile

Workspaces

{
    "workspaces": ["packages/*"]
}

Bundler

Basic Usage

// JavaScript API
await Bun.build({
    entrypoints: ["./src/index.ts"],
    outdir: "./dist",
    target: "browser", // "browser" | "bun" | "node"
    minify: true,
    sourcemap: "linked",
});
# CLI
bun build ./src/index.ts --outdir ./dist --minify

Options

Option Description
--target browser, bun, node
--format esm, cjs, iife
--minify Enable minification
--sourcemap none, linked, inline, external
--splitting Code splitting
--external Exclude packages

Standalone Executables

bun build ./cli.ts --compile --outfile mycli
./mycli

Test Runner

Writing Tests

import { test, expect, describe, beforeAll, mock } from "bun:test";

describe("math", () => {
    test("adds numbers", () => {
        expect(2 + 2).toBe(4);
    });

    test("async test", async () => {
        const result = await fetchData();
        expect(result).toBeDefined();
    });
});

Mocking

import { mock, spyOn } from "bun:test";

const fn = mock(() => 42);
fn();
expect(fn).toHaveBeenCalled();

// Spy on object method
const spy = spyOn(console, "log");

CLI Options

bun test                          # Run all
bun test --watch                  # Watch mode
bun test --coverage               # Coverage
bun test -t "pattern"             # Filter by name
bun test --timeout 10000          # Set timeout
bun test --bail                   # Stop on first failure
bun test --update-snapshots       # Update snapshots

Configuration (bunfig.toml)

# Runtime
preload = ["./setup.ts"]
logLevel = "debug"

[define]
"process.env.NODE_ENV" = "'production'"

# Package Manager
[install]
production = false
frozenLockfile = false
exact = false

[install.scopes]
myorg = { token = "$NPM_TOKEN", url = "https://registry.myorg.com/" }

# Test Runner
[test]
root = "./__tests__"
preload = ["./test-setup.ts"]
coverage = true
coverageThreshold = 0.8

Framework Integration

Next.js

bunx create-next-app my-app
cd my-app && bun dev

React

bun create react my-app

Express

import express from "express";
const app = express();
app.listen(3000);

TypeScript

Native support, no config needed. For types:

bun add -d @types/bun

Node.js Compatibility

Bun aims for Node.js compatibility. Most node:* modules work:

  • node:fs, node:path, node:http, node:crypto, etc.

Check compatibility status for specifics.

Documentation References

Core

Runtime

Built-in Modules

HTTP & Networking

Package Manager

Bundler

Test Runner

Framework Guides

Database Guides

Deployment

Common Task Guides

Templates & Scaffolding

Project Links

Weekly Installs
3
First Seen
2 days ago
Installed on
opencode3
claude-code3
codex3
gemini-cli3
cursor3
github-copilot2