Skip to content
Tutorials Center
My learning

Topics

Bun Tutorial

Learn Bun step by step: 39 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Bun? Learn what Bun is - a fast JavaScript and TypeScript runtime with a package manager, bundler and test runner built in - how it works, and when to choose it over Node.js.
  2. 2 Installing Bun Install Bun on Windows, macOS, Linux, Docker or through npm, check the version, upgrade, pin a version in CI, set up editor types and uninstall cleanly.
  3. 3 Your First Bun Program Create a project with bun init, understand every generated file, run TypeScript with bun run, read command-line arguments and print output - your first complete Bun program.
  4. 4 A Tour of the Bun CLI A guided tour of every important Bun command - run, install, add, build, test, bunx, create, repl, upgrade - plus global flags and the bunfig.toml configuration file.

Runtime Basics

  1. 5 TypeScript and JSX in Bun Run TypeScript and JSX in Bun with no build step - how type stripping works, what is not checked, the recommended tsconfig.json, path aliases, enums and decorators, and JSX settings.
  2. 6 Modules and Imports in Bun Learn how Bun resolves modules - ES modules and CommonJS side by side, top-level await, import.meta.dir and main, importing JSON and text files, node: and bun: specifiers.
  3. 7 Environment Variables and Configuration Read environment variables in Bun with Bun.env and process.env, rely on automatic .env loading, choose files per environment, expand variables, validate config and keep secrets safe.
  4. 8 Web APIs and Built-in Utilities Use the web-standard globals Bun implements (fetch, URL, streams, crypto) and its own utilities - Bun.sleep, Bun.hash, Bun.password, Bun.spawn and the Bun.$ shell - with worked examples.
  5. 9 Watch Mode and Hot Reloading Speed up development with bun --watch and bun --hot - how each works, when to choose which, keeping state across reloads with globalThis, test watch mode and what files are watched.

Package Manager and Scripts

  1. 10 Installing and Adding Packages Use bun install, add, remove, update and outdated to manage npm packages - version ranges, dev and peer dependencies, git and local packages, global installs, production installs and why Bun is fast.
  2. 11 package.json, the Lockfile and the Cache Understand package.json fields, the text bun.lock lockfile, migration from npm, yarn and pnpm, lifecycle script safety with trustedDependencies, bun pm commands, why and audit, and the global cache.
  3. 12 Scripts and bunx Define package.json scripts and run them with bun run, use pre and post hooks, pass arguments, run local binaries and one-off tools with bunx, and force the Bun runtime with --bun.
  4. 13 Workspaces and Catalogs Build a monorepo with Bun workspaces - package globs, the workspace protocol, shared dependencies, catalogs for one version of a library, filtered scripts, installs and publishing.
  5. 14 Overrides, Patches and Registries Fix dependency problems in Bun - force versions with overrides, patch a package with bun patch, configure private registries and scopes in bunfig.toml or .npmrc, and publish with bun publish.

Bundler and Transpiler

  1. 15 Bun Build Basics Learn what a bundler does and use bun build and the Bun.build API to bundle TypeScript for the browser, Bun or Node - entry points, outputs, targets, watch mode and reading build results.
  2. 16 Build Options: Minify, Sourcemaps, Splitting Master the main bun build options - minify, sourcemap, splitting, external, define and env, naming with hashes, public paths, drop, banner and format - with CLI and Bun.build examples.
  3. 17 Loaders and HTML Entry Points Learn how Bun loaders turn JSON, TOML, text, CSS, images and WebAssembly into modules, choose loaders per extension, and bundle a whole web page starting from an HTML file.
  4. 18 Plugins, Macros and the Transpiler Extend Bun with plugins (onResolve, onLoad), run them in builds and at run time, compute values at bundle time with macros, and transform or scan code with the Bun.Transpiler API.
  5. 19 Single-File Executables Turn a Bun program into one standalone executable with bun build --compile - cross-compile for Linux, macOS and Windows, embed assets, set minify and bytecode, and know the size and limits.

HTTP Servers and APIs

  1. 20 Your First Server with Bun.serve Start an HTTP server with Bun.serve: the fetch handler, Request and Response, ports, hostname, error handling, server.stop and hot reload, with runnable Node stand-ins.
  2. 21 Routes, Parameters and Method Handlers Use the routes object in Bun.serve: static responses, :id parameters, wildcards, per-method handlers, a fallback fetch and 404/405 answers, with a runnable Node stand-in.
  3. 22 Request Bodies: JSON, Forms and Uploads Read request bodies in Bun.serve: req.json, req.text, req.formData, multipart file uploads, body size limits and input validation, with runnable Node stand-ins.
  4. 23 Headers, Cookies and Calling Other APIs with fetch Set and read headers, handle CORS and cookies in Bun servers, and call other APIs with fetch: timeouts, retries, error handling and Bun-specific options.
  5. 24 Streaming Responses and Server-Sent Events Stream data from a Bun server with ReadableStream, serve large files with Bun.file, push live updates with Server-Sent Events and handle client disconnects.
  6. 25 WebSockets and Pub/Sub Build real-time features with Bun's built-in WebSocket server: server.upgrade, open/message/close handlers, per-socket data, topics with subscribe and publish, and a runnable chat stand-in.

Testing

  1. 26 Your First Tests with bun test Write tests with Bun's built-in Jest-compatible runner: test, describe, expect matchers, async tests, test.each, skipping and filtering, plus a runnable mini test runner.
  2. 27 Mocks, Spies and Fake Time Isolate code in bun test with mock functions, spyOn, mock.module and setSystemTime: call tracking, return values, restoring state and testing code that uses fetch or the clock.
  3. 28 Hooks, Snapshots, Coverage and Testing a Server Set up and tear down with beforeAll/beforeEach, use snapshot tests, measure coverage, preload files and test a Bun.serve API on port 0 with fetch.

File I/O, SQLite and Data

  1. 29 Bun.file and Bun.write: Fast File I/O Read and write files with Bun.file and Bun.write: lazy BunFile objects, text, json, bytes and streams, incremental writers, directories with node:fs and safe path handling.
  2. 30 SQLite with bun:sqlite Use Bun's built-in SQLite driver: Database, query vs prepare, all/get/run, named parameters, transactions, WAL mode, migrations and safe SQL, with a runnable node:sqlite stand-in.
  3. 31 Bun Shell and Child Processes Run commands from JavaScript with the Bun Shell tagged template and Bun.spawn: safe interpolation, pipes, output helpers, exit codes, environment, working directory and streaming output.
  4. 32 Passwords, Hashing, Environment and Other Data Clients Hash passwords with Bun.password, compute digests with CryptoHasher, load .env files and Bun.env, compress data, and meet Bun's built-in Postgres, Redis and S3 clients.

Deploy and Compatibility

  1. 33 Node.js Compatibility and Migrating a Project What works the same in Bun and Node: built-in modules, npm packages, CommonJS and ESM, globals and import.meta. Learn the differences and migrate a Node project step by step.
  2. 34 Docker and Single-File Executables Ship a Bun app: a multi-stage Dockerfile with the official image, frozen lockfile installs, non-root users, compile to one executable with bun build --compile, cross-targets and deployment checklists.
  3. 35 Production Hardening, Workers and Performance Run Bun services reliably: graceful shutdown on SIGTERM, structured logging, timeouts and limits, Web Workers for CPU-bound work, reusePort, memory flags, measuring performance and a launch checklist.

Projects and Practice

  1. 36 Project: A REST API with Bun and SQLite Build a complete notes REST API: project layout, SQLite schema, Bun.serve routes, validation, pagination, error handling and tests, with the full logic running on a Node stand-in.
  2. 37 Project: A Link Shortener Build a link shortener with Bun.serve: validate URLs, generate short codes, store them in SQLite, redirect with 302, count clicks, add a simple rate limiter and an HTML form.
  3. 38 Project: A Command-Line Tool Build a todo-scanner CLI with Bun: parse arguments with util.parseArgs, scan files with Bun.Glob, format output, set exit codes, read stdin, add a shebang and compile to a single executable.
  4. 39 Practice Exercises and Challenges Eight hands-on Bun exercises with hints and solutions: routing, validation, token-bucket rate limiting, SQLite queries, streaming CSV, mock-based tests, a config loader and a capstone.