Bun JavaScript Runtime

1.3.12 · active · verified Sun Apr 19

Bun is an all-in-one JavaScript runtime, bundler, transpiler, and package manager, designed for extreme speed and a streamlined developer experience. It is built on Apple's WebKit engine's JavaScriptCore and written in Zig. As of version 1.3.12, Bun offers a highly performant alternative to Node.js, providing native support for TypeScript, JSX, and ES modules out of the box. The project maintains a rapid release cadence with frequent patch and minor versions, often weekly, addressing bugs and introducing new features. Its key differentiators include significantly faster startup times, execution speed, and comprehensive tooling built into a single executable, aiming to replace a multitude of separate tools like Node.js, npm/yarn/pnpm, webpack, Babel, and esbuild. Bun is currently stable for production use but continues to evolve rapidly, adding new features and improving compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple HTTP server using Bun's built-in `Bun.serve` API, handling basic routing and errors. It uses web-standard `Request` and `Response` objects and shows type-safety with TypeScript, which Bun supports natively.

import { Server, serve } from 'bun';

interface Env {
  PORT?: string;
}

const env = process.env as Env;
const port = env.PORT ? parseInt(env.PORT, 10) : 3000;

const server = Bun.serve({
  port: port,
  fetch(request: Request): Response {
    const url = new URL(request.url);
    if (url.pathname === '/') {
      return new Response('Hello from Bun! Visit /greet/:name');
    }
    if (url.pathname.startsWith('/greet/')) {
      const name = url.pathname.split('/').pop();
      return new Response(`Greetings, ${name}!`);
    }
    return new Response('404 Not Found', { status: 404 });
  },
  error(error: Error): Response {
    console.error('Server error:', error);
    return new Response('Internal Server Error', { status: 500 });
  },
});

console.log(`Bun server listening on http://localhost:${server.port}`);

view raw JSON →