Skip to main content
Bun includes built-in debugger support compatible with Chrome DevTools, VS Code, and other debugging clients using the WebSocket Inspector protocol.

Quick Start

Start Bun with debugging enabled:
Debug mode
Connect with Chrome DevTools:
  1. Open chrome://inspect in Chrome
  2. Click “Configure” and add localhost:6499
  3. Click “inspect” on your Bun process

Inspector Options

—inspect

Start inspector on default port (6499):
Default inspector
Behavior:
  • Starts WebSocket inspector server
  • Continues execution immediately
  • Debugger can attach at any time

—inspect-wait

Wait for debugger before executing:
Wait for debugger
Behavior:
  • Starts inspector
  • Pauses execution until debugger attaches
  • Useful for debugging initialization code

—inspect-brk

Break on first line:
Break on start
Behavior:
  • Starts inspector
  • Pauses on first line of code
  • Allows setting breakpoints before execution

Custom Port

Specify inspector port:
Custom port

Chrome DevTools

Use Chrome’s built-in debugger:

Setup

  1. Start Bun with --inspect:
  2. Open Chrome and navigate to:
  3. Under “Remote Target”, click Configure
  4. Add inspector URL:
  5. Click Inspect on your Bun process

Features

  • View source files with TypeScript support
  • Set breakpoints by clicking line numbers
  • Step through code (F10, F11)
  • Watch expressions
  • Call stack navigation
  • Evaluate expressions in paused context
  • View console.log output
  • Inspect objects interactively
  • Execute code at breakpoints
  • CPU profiling
  • Memory heap snapshots
  • Performance timeline
  • HTTP request/response inspection
  • WebSocket frame monitoring
  • Timing analysis

VS Code Debugging

Integrate with Visual Studio Code:

Launch Configuration

Create .vscode/launch.json:
.vscode/launch.json

Attach Configuration

Attach to running Bun process:
Attach configuration

Debug Current File

Debug current file

Debugging Tests

Debug tests

Usage

  1. Set breakpoints: Click in the gutter next to line numbers
  2. Start debugging: Press F5 or click “Run and Debug”
  3. Step through: Use F10 (step over), F11 (step into), Shift+F11 (step out)
  4. Inspect variables: Hover over variables or use the Variables panel
  5. Evaluate expressions: Use the Debug Console

Breakpoints

Line Breakpoints

Line breakpoints

Conditional Breakpoints

Conditional breakpoint

Logpoints

Log without stopping:
Logpoint

Programmatic Breakpoints

Debugger statement

Debugging Features

Call Stack

Navigate function call hierarchy:
Call stack

Variable Inspection

Inspect variables

Watch Expressions

Monitor expressions across breakpoints:
Watch expressions

Console Evaluation

Evaluate code while paused:
Console evaluation

Source Maps

Bun automatically generates source maps for TypeScript:
Original TypeScript
Debugger shows original TypeScript, not transpiled JavaScript.

Inline Source Maps

Bun embeds source maps by default:
Generated output

External Source Maps

Generate separate .map files:
External source maps

Async Debugging

Debug async code:
Async debugging
Features:
  • Step through promises
  • Inspect promise state
  • View async call stack

Exception Breaking

Pause on thrown exceptions:

DevTools

  1. Open Sources panel
  2. Click “Pause on exceptions” button (⏸)
  3. Choose:
    • Pause on caught exceptions
    • Pause on uncaught exceptions

VS Code

Breakpoints on exceptions

Performance Profiling

CPU Profile

Profile CPU usage:
  1. Start Bun with --inspect
  2. Open Chrome DevTools
  3. Go to Profiler tab
  4. Click Record
  5. Run your code
  6. Click Stop
  7. Analyze flame graph

Memory Profiling

Analyze memory usage:
  1. Open Chrome DevTools
  2. Go to Memory tab
  3. Select Heap snapshot
  4. Click Take snapshot
  5. Inspect objects and allocations

Remote Debugging

Debug Bun running on remote server:

SSH Tunnel

SSH tunnel
Connect to localhost:6499 from Chrome DevTools or VS Code.

Network Access

Allow network access
Only expose inspector on trusted networks! No authentication by default.

Implementation

Debugger implementation:
  • Inspector: src/js/internal/debugger.ts - WebSocket inspector server
  • Protocol: Chrome DevTools Protocol (CDP)
  • Source maps: src/sourcemap/ - Source map generation
  • Runtime: src/runtime.zig:176 - Set breakpoint on first line flag
Debugger flag (src/runtime.zig:176)

Advanced Debugging

Multi-Process Debugging

Debug multiple Bun processes:
Multiple processes
Attach separate debuggers to each process.

Debugging Workers

Worker debugging
Start with --inspect to debug workers.

Debugging Tests

Debug tests
Sets breakpoint before test execution.

Common Workflows

Debug HTTP Server

Debug server

Debug CLI Tool

Debug CLI

Debug Build Script

Debug build

Troubleshooting

Debugger Won’t Connect

  1. Check port: Ensure port 6499 is not in use
  2. Check firewall: Allow connections to inspector port
  3. Check URL: Verify ws://localhost:6499 in DevTools
Check port

Breakpoints Not Hitting

  1. Check source maps: Ensure TypeScript files map correctly
  2. Verify file path: Use absolute paths in launch.json
  3. Clear cache: Remove .bun cache directory
Clear cache

Source Maps Not Working

  1. Check inline maps: Bun generates inline maps by default
  2. Verify tsconfig: Check sourceMap option
  3. Check paths: Ensure file paths are absolute

VS Code Can’t Find Source

Source map locations

Debugging Tips

1. Use Conditional Breakpoints

Instead of:
Use conditional breakpoint: i === 50

2. Logpoints Over console.log

Avoid modifying code:
  • Set logpoint instead of console.log
  • Message: User {user.id} - {user.name}

3. Watch Expressions

Monitor complex expressions:
  • user?.profile?.settings
  • items.filter(x => x.active).length

4. Copy Value

Right-click variable → Copy Value (in VS Code)

5. Restart Frame

Right-click call stack frame → Restart Frame (re-execute function)

Performance

Inspector overhead:
  • Minimal when not attached: <1% overhead
  • When attached: ~5-10% depending on breakpoints
  • Profiling active: ~20% overhead
Production recommendation: Disable inspector in production.

Security

Never expose inspector port publicly! No authentication mechanism.
Security best practices:
  1. Bind to localhost: --inspect=127.0.0.1:6499
  2. Use SSH tunnels: For remote debugging
  3. Firewall rules: Block inspector port from internet
  4. Disable in production: Remove --inspect flag