DEV Community

Alex Spinov
Alex Spinov

Posted on

Bun Has a Free API: The All-in-One JavaScript Runtime That Replaces Node, npm, and Webpack

Bun is a JavaScript/TypeScript runtime that bundles a package manager, bundler, test runner, and HTTP server into one fast executable — replacing Node.js, npm, webpack, and jest.

Why Bun Matters

Node.js ecosystem requires: Node.js + npm + webpack/vite + jest + ts-node. Bun replaces ALL of them with one tool that's 3-4x faster.

What you get for free:

  • JavaScript/TypeScript runtime (Node.js compatible)
  • Package manager (3-25x faster than npm)
  • Bundler (replaces webpack/esbuild)
  • Test runner (replaces jest)
  • Native TypeScript support (no ts-node)
  • Built-in SQLite
  • Built-in S3 client
  • Web API compatibility (fetch, WebSocket, etc.)

Quick Start

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

bun init          # Create project
bun install       # Install deps (3-25x faster than npm)
bun run index.ts  # Run TypeScript directly
bun test          # Run tests
bun build ./src/index.ts --outdir ./dist  # Bundle
Enter fullscreen mode Exit fullscreen mode

HTTP Server

Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === "/api/hello") {
      return Response.json({ message: "Hello from Bun!" });
    }
    if (url.pathname === "/api/users" && req.method === "POST") {
      const body = await req.json();
      return Response.json({ created: body }, { status: 201 });
    }
    return new Response("Not Found", { status: 404 });
  },
});
console.log("Server running on http://localhost:3000");
Enter fullscreen mode Exit fullscreen mode

Built-in SQLite

import { Database } from "bun:sqlite";

const db = new Database("app.db");

db.run(`
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
  )
`);

const insert = db.prepare("INSERT INTO users (name, email) VALUES (?, ?)");
insert.run("Alice", "alice@example.com");

const users = db.prepare("SELECT * FROM users").all();
console.log(users);
Enter fullscreen mode Exit fullscreen mode

File I/O

// Write file
await Bun.write("output.txt", "Hello World");
await Bun.write("data.json", JSON.stringify({ key: "value" }));

// Read file
const text = await Bun.file("output.txt").text();
const json = await Bun.file("data.json").json();

// Glob
const glob = new Bun.Glob("**/*.ts");
for await (const file of glob.scan("./src")) {
  console.log(file);
}
Enter fullscreen mode Exit fullscreen mode

Testing

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

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

  test("async", async () => {
    const res = await fetch("http://localhost:3000/api/hello");
    expect(res.status).toBe(200);
    const data = await res.json();
    expect(data.message).toBe("Hello from Bun!");
  });
});
Enter fullscreen mode Exit fullscreen mode

Speed Comparison

Task npm Bun
Install (clean) 30s 3s
Install (cached) 8s 0.5s
Run TypeScript 2s (ts-node) 0.1s
HTTP req/sec 70K (Node) 250K
SQLite ops/sec N/A 1M+

Links


Building fast JS/TS tools? Check out my developer tools on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)