bunjs

Use Bun as runtime, package manager, bundler, and test runner. Use when running JS/TS with bun, installing packages with bun install, bundling with bun build, testing with bun test, or when user mentions Bun, bunfig.toml, or bun.lock.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "bunjs" with this command: npx skills add migueldialpad/skills/migueldialpad-skills-bunjs

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

CommandDescription
bun installInstall 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 updateUpdate all
bun outdatedCheck 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

OptionDescription
--targetbrowser, bun, node
--formatesm, cjs, iife
--minifyEnable minification
--sourcemapnone, linked, inline, external
--splittingCode splitting
--externalExclude 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

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

nextjs

No summary provided by upstream source.

Repository SourceNeeds Review
General

mermaid-charts

No summary provided by upstream source.

Repository SourceNeeds Review
General

astro-framework

No summary provided by upstream source.

Repository SourceNeeds Review
General

webos-tv

No summary provided by upstream source.

Repository SourceNeeds Review