Bun Documentation - Runtime, Package Manager, and Bundler

website·Indexed Apr 12, 2026

https://bun.sh/docs/

Official documentation for Bun, a fast JavaScript runtime and toolchain serving as a drop-in replacement for Node.js. Covers the Bun Package Manager (bun install), native bundler (bun build), and built-in test runner. Includes guides on installation, TypeScript configuration, runtime features like watch mode and debugging, HTTP server setup with TLS and WebSockets, and file system routing. Supports JavaScriptCore engine execution, plugin APIs, and integration with frameworks like Astro and React.

Tokens
8.9K
Snippets
46
Records
102
Agent score
50%

What's inside Bun

  1. Automatic sourcemap generation for debugging

    Bun automatically generates and serves sourcemapped files for every file it transpiles. Stack traces in the console point to original source files (TypeScript, JSX, etc.) rather than transpiled output. Sourcemaps are loaded both at runtime during on-demand transpilation and when using bun build for precompilation.
  2. Bun project roadmap and scope

    Bun is an early-stage project with a large scope, aiming to provide an all-in-one toolkit that replaces fragmented JavaScript/TypeScript toolchains including Node.js, Jest, Webpack, esbuild, Babel, yarn, and PostCSS. For detailed long-term plans and priorities, refer to the official GitHub roadmap issue.
  3. V8-compatible stack trace formatting

    Bun uses JavaScriptCore as its engine but formats error.stack identically to Node.js's V8 engine. This ensures compatibility with npm libraries that expect V8 stack trace formatting. The V8 Stack Trace API is implemented for customizing stack trace output.
  4. Quickstart prerequisites

    Bun must be installed and available on your PATH before following the quickstart. See the installation guide for setup instructions.
  5. Fix GCC 'span' file not found on Ubuntu

    Bun requires C++20 features like std::span not available in GCC < 11. If bun setup fails with 'span' file not found, install GCC 11: sudo apt update && sudo apt install gcc-11 g++-11. If the package isn't found, add the repository: sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test first. Then set as default: sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 and sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100.
  6. Production bundling strategies

    Two production options: (1) Ahead of Time Bundling (recommended) — run bun build --target=bun --production --outdir=dist ./src/index.ts before deployment. Bun bundles HTML imports into a manifest object Bun.serve() uses directly. (2) Runtime Bundling — set development: false to enable in-memory caching of lazily-bundled assets, Cache-Control/ETag headers, and minification.
  7. Create a SvelteKit project with Bun

    Use bunx sv create my-app to create a new SvelteKit project. The CLI prompts you to select a template, choose TypeScript support, and select Bun as the package manager. Dependencies are installed automatically during setup.
  8. Allow non-root users to bind to ports 80 and 443

    Non-root users cannot bind to privileged ports (80, 443) by default. To allow Bun to bind to these ports when run by a non-root user, run: setcap CAP_NET_BIND_SERVICE=+eip ~/.bun/bin/bun. This permanently grants the capability to the bun binary. Skip this step if running as root.
  9. Response properties preserved during HTML transformation

    When transforming a Response with HTMLRewriter, the following properties are automatically preserved: status code, headers, and other response metadata. The body is transformed while maintaining streaming capabilities, and content-encoding (such as gzip) is handled automatically. Headers are cloned to the new response. The original response body is marked as consumed after transformation.
  10. Execute SQL queries with the neon() function

    Import neon from @neondatabase/serverless and call it with process.env.DATABASE_URL to create a SQL client. Execute queries using tagged template literals. The client uses SQL-over-HTTP by default. Run with bun ./index.ts.

    import { neon } from "@neondatabase/serverless";

    const sql = neon(process.env.DATABASE_URL); const rows = await sqlSELECT version(); console.log(rows[0].version);

  11. Convert relative URLs to absolute with URL constructor

    When scraping, relative URLs like /docs need conversion to absolute URLs. Use new URL(href, baseUrl).href to resolve relative paths against the page's origin. Wrap in try/catch to handle invalid URLs gracefully. The URL constructor automatically handles protocols, domains, and path resolution.

    const absoluteURL = new URL(href, url).href; // "/docs" + "https://example.com" → "https://example.com/docs"