DEV Community

Alex Spinov
Alex Spinov

Posted on

Bun Has a Free API: The All-in-One JavaScript Runtime That Replaces Node.js

Why Bun

Bun is a JavaScript runtime, package manager, bundler, and test runner — all in one. Written in Zig with JavaScriptCore engine, it is 3-10x faster than Node.js for most tasks.

Install

curl -fsSL https://bun.sh/install | bash
Enter fullscreen mode Exit fullscreen mode

Run JavaScript/TypeScript

# Run TypeScript directly — no tsc needed
bun run server.ts

# Run JSX
bun run app.tsx
Enter fullscreen mode Exit fullscreen mode

Package Manager (Fastest)

# Install dependencies (10-100x faster than npm)
bun install

# Add package
bun add express

# Dev dependency
bun add -d typescript
Enter fullscreen mode Exit fullscreen mode

HTTP Server

const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === '/api/users') {
      const users = await getUsers();
      return Response.json(users);
    }
    return new Response('Hello from Bun!', { status: 200 });
  },
});

console.log(`Server running at http://localhost:${server.port}`);
Enter fullscreen mode Exit fullscreen mode

Built-in SQLite

import { Database } from 'bun:sqlite';

const db = new Database('myapp.db');
db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)');
db.run('INSERT INTO users (name) VALUES (?)', ['Alice']);
const users = db.query('SELECT * FROM users').all();
Enter fullscreen mode Exit fullscreen mode

Test Runner

import { test, expect } from 'bun:test';

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

test('fetch works', async () => {
  const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  expect(res.status).toBe(200);
});
Enter fullscreen mode Exit fullscreen mode
bun test
Enter fullscreen mode Exit fullscreen mode

Bundler

bun build ./src/index.ts --outdir ./dist --target browser
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Runtime — Node.js compatible, runs TypeScript natively
  • Package manager — 10-100x faster than npm
  • Bundler — built-in, replaces webpack/esbuild
  • Test runner — Jest-compatible, built-in
  • SQLite — built-in database
  • File I/O — Bun.file() is 10x faster than fs
  • MIT license — free

Resources


Need to extract JavaScript ecosystem data, npm stats, or runtime benchmarks? Check out my Apify tools or email spinov001@gmail.com for custom solutions.

Top comments (0)