Bun Documentation

website·Indexed 39 days ago

https://bun.com/

Documentation for Bun, a fast JavaScript runtime, package manager, and bundler. Includes guides on using the `bun build` CLI and `Bun.build()` API, bytecode caching, CSS support, minification, and creating single-file executables. Also features deployment guides for AWS Lambda and DigitalOcean, and detailed binary data conversion tutorials for ArrayBuffer, Blob, and Buffer.

Tokens
224.8K
Snippets
1.5K
Records
1.8K
Agent score
74%

What's inside Bun

  1. Overview of Bun Bindgen for internal contributors

    Bindgen is an internal tool for Bun maintainers and contributors. It scans for *.bind.ts files to find function and class definitions and generates the necessary glue code to enable interop between JavaScript and native Rust code. It is intended to replace older systems like the 'Classes generator' (*.classes.ts) and 'JS2Native'.
  2. Overview of Bun native APIs

    Bun provides a set of optimized native APIs available via the Bun global object and built-in modules. While Bun implements standard Web APIs wherever possible, it introduces native APIs for server-side tasks that lack a standard (such as file I/O and HTTP servers), often building upon standard types like Blob, URL, and Request.
  3. Overview of Hot Module Replacement (HMR) in Bun

    Hot Module Replacement (HMR) allows Bun's development server to update modules in a running application without requiring a full page reload, which preserves the application state. HMR is enabled by default when using Bun's full-stack development server. To disable HMR in Bun.serve, set the development option to { hmr: false }.
  4. Overview of Bun Secrets API

    Bun.secrets is an experimental, asynchronous, non-blocking API used to manage sensitive credentials for local development tools and CLIs. Instead of storing secrets in plaintext files (like .env or ~/.npmrc), it leverages native OS storage:

    • macOS: Keychain Services
    • Linux: libsecret (GNOME Keyring, KWallet)
    • Windows: Windows Credential Manager

    It is primarily intended for local development. For production deployments, environment variables or dedicated secret management services are recommended.

  5. Overview of Bun's Semver API

    Bun provides a built-in semantic versioning API via Bun.semver that is designed to be compatible with node-semver (used by npm clients) but is significantly faster (approximately 20x). It is used to compare semantic versions and check if a version satisfies a specific range.
  6. Overview of Bun binary data classes

    Bun supports several classes for binary data manipulation, mixing Web-standard APIs and Node.js/Bun-specific extensions:

    • TypedArray: Web-standard family of classes (e.g., Uint8Array) for array-like binary access.
    • Buffer: Node.js API (subclass of Uint8Array) providing convenience methods; not available in browsers.
    • DataView: Web-standard low-level API for reading/writing specific byte offsets.
    • Blob: Web-standard readonly blob of binary data (often files) with MIME type and size.
    • File: Web-standard subclass of Blob representing a file with a name and timestamp.
    • BunFile: Bun-specific subclass of Blob for lazily-loaded files on disk, created via Bun.file(path).
  7. Overview of Bun's built-in CSS capabilities

    Bun's bundler includes native support for several CSS features, including transpiling modern/future CSS features (with vendor prefixing), minification, CSS Modules, and Tailwind CSS (via a native bundler plugin). Transpiling and vendor prefixing are enabled by default.
  8. Overview of Bun Security Scanner API

    Bun's package manager can integrate security scanners to analyze packages during bun install, bun add, and other package operations. These scanners help detect known security vulnerabilities (CVEs), malicious packages, and license compliance issues. Depending on the severity level reported by the scanner, Bun will react differently:

    • fatal: Installation stops immediately and exits with a non-zero code.
    • warn: In interactive terminals, the user is prompted to continue; in CI environments, the process exits immediately.
  9. Overview of Bun toolkit capabilities

    Bun is a comprehensive toolkit for modern JavaScript and TypeScript applications, delivered as a single, dependency-free binary. It serves as a fast, drop-in replacement for Node.js and integrates several development tools into one executable:

    • Runtime: Executes JS/TS files with near-zero overhead, powered by JavaScriptCore.
    • Package Manager: A high-performance alternative to npm with a global cache and workspace support.
    • Test Runner: A Jest-compatible, TypeScript-first runner featuring snapshots, DOM support, and watch mode.
    • Bundler: Native bundling for JS, TS, JSX, and CSS for both server and browser targets.
  10. Overview of Bun's Universal Plugin API

    Bun provides a universal plugin API that extends both the runtime and the bundler. Plugins allow developers to intercept imports and implement custom loading logic, such as adding support for new file types (e.g., .scss), implementing CSS extraction, creating macros, or handling client-server code co-location.
  11. Install dependencies for a different platform or architecture

    Use the --cpu and --os flags to override the target platform for package selection. This is useful for cross-platform builds or preparing deployments for different environments.
    bun install --cpu=x64 --os=linux
  12. Use Bun's unified SQL API for database queries

    Bun provides a unified Promise-based API for PostgreSQL, MySQL, and SQLite using tagged template literals. This approach protects against SQL injection by automatically handling parameter escaping. The API supports connection pooling, transactions, prepared statements, and BigInt support.
    import { sql, SQL } from "bun";
    
    // PostgreSQL (default)
    const users = await sql`SELECT * FROM users WHERE active = ${true} LIMIT ${10}`;
    
    // MySQL
    const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
    const mysqlResults = await mysql`SELECT * FROM users WHERE active = ${true}`;
    
    // SQLite
    const sqlite = new SQL("sqlite://myapp.db");
    const sqliteResults = await sqlite`SELECT * FROM users WHERE active = ${1}`;