| name | typescript-debugging |
| description | Modern TypeScript/JavaScript debugging with Bun — inspector flags, debug.bun.sh, VSCode launch.json, memory profiling, heap analysis. Use when setting up interactive debugging, investigating leaks, CPU profiling with `--cpu-prof`, or sourcemaps. |
| user-invocable | false |
| allowed-tools | Bash, Read, Write, Edit, Grep, Glob, TodoWrite |
| created | "2026-01-22T00:00:00.000Z" |
| modified | "2026-05-09T00:00:00.000Z" |
| reviewed | "2026-01-22T00:00:00.000Z" |
TypeScript Debugging
When to Use This Skill
| Scenario | Use this skill | Alternative |
|---|
| Setting up Bun inspector for debugging | Yes | N/A |
| Configuring VSCode launch.json for Bun | Yes | N/A |
| Investigating memory leaks with heap snapshots | Yes | N/A |
| CPU profiling TypeScript applications | Yes | N/A |
| Debugging network requests with verbose fetch | Yes | N/A |
| Setting up sourcemaps for debugging | Yes | bun-development for build-time sourcemap flags |
| Monitoring errors in production | No - use typescript-sentry | N/A |
| Running tests to find failures | No - use bun-development | bun-test for quick test runs |
Core Expertise
Modern debugging for TypeScript/JavaScript with Bun runtime:
- WebKit Inspector Protocol (debug.bun.sh)
- VSCode integration with Bun extension
- Memory profiling with V8 heap snapshots
- Automatic sourcemap generation for TypeScript
- Chrome DevTools for heap analysis
Inspector Flags
Basic Debugging
bun --inspect script.ts
bun --inspect=4000 script.ts
bun --inspect=localhost:4000 script.ts
Break on Start
bun --inspect-brk script.ts
bun --inspect-wait script.ts
Debugging Tests
bun --inspect test
bun --inspect-brk test auth.test.ts
Web Debugger (debug.bun.sh)
Bun's built-in web debugger is a modified WebKit Web Inspector:
bun --inspect script.ts
Features
| Feature | Description |
|---|
| Source view | View original TypeScript/JSX with sourcemaps |
| Breakpoints | Click line numbers to set/remove |
| Console | Execute code in current context |
| Call stack | Inspect execution frames |
| Scope | View local/closure/global variables |
| Watch | Add expressions to monitor |
Execution Controls
| Control | Action |
|---|
| Continue (F8) | Run until next breakpoint |
| Step Over (F10) | Execute line, skip into functions |
| Step Into (F11) | Enter function call |
| Step Out (Shift+F11) | Complete function, return to caller |
VSCode Integration
Extension Setup
Install Bun for Visual Studio Code.
launch.json Configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "bun",
"request": "launch",
"name": "Debug Script",
"program": "${workspaceFolder}/src/index.ts",
"cwd": "${workspaceFolder}",
"stopOnEntry": false,
"watchMode": false
},
{
"type": "bun",
"request": "launch",
"name": "Debug Tests",
"program": "${workspaceFolder}/tests/index.test.ts",
Configuration Options
| Option | Type | Description |
|---|
program | string | Entry file path |
cwd | string | Working directory |
args | string[] | Arguments to script |
env | object | Environment variables |
stopOnEntry | boolean | Break at first line |
watchMode | boolean | Enable --watch/--hot |
noDebug | boolean | Run without debugger |
strictEnv | boolean | Only use specified env |
Memory Debugging
Heap Snapshots
Create snapshots for Chrome DevTools analysis:
import { writeHeapSnapshot } from "v8";
writeHeapSnapshot("before.heapsnapshot");
doSomeWork();
writeHeapSnapshot("after.heapsnapshot");
Load .heapsnapshot files in Chrome DevTools Memory tab for comparison.
Heap Statistics (bun:jsc)
import { heapStats } from "bun:jsc";
const stats = heapStats();
console.log({
heapSize: stats.heapSize,
objectCount: stats.objectCount,
objectTypeCounts: stats.objectTypeCounts
});
Process Memory
console.log(process.memoryUsage.rss());
console.log(process.memoryUsage());
Non-JS Memory (mimalloc)
MIMALLOC_SHOW_STATS=1 bun script.ts
Performance Profiling
CPU Profiling
bun --cpu-prof script.ts
Network Request Debugging
BUN_CONFIG_VERBOSE_FETCH=1 bun script.ts
BUN_CONFIG_VERBOSE_FETCH=curl bun script.ts
Sourcemaps
Bun automatically generates sourcemaps for transpiled files:
- TypeScript → JavaScript mapping preserved
- JSX transformations tracked
- Stack traces show original source locations
- Debugger shows TypeScript, not transpiled JS
Build with Sourcemaps
bun build ./src/index.ts --outdir=dist --sourcemap=external
bun build ./src/index.ts --outdir=dist --sourcemap=inline
bun build ./src/index.ts --outdir=dist --sourcemap=none
Console Debugging
Beyond console.log
console.table([{ id: 1, name: "a" }, { id: 2, name: "b" }]);
console.time("fetch");
await fetch(url);
console.timeEnd("fetch");
console.group("Request");
console.log("URL:", url);
console.log("Method:", method);
console.groupEnd();
console.assert(value > 0, "Value must be positive", value);
console.trace("Reached here");
Programmatic Breakpoints
debugger;
if (suspiciousCondition) {
debugger;
}
Common Leak Patterns
Closure Entrapment
const largeData = loadHugeArray();
setInterval(() => {
console.log(largeData.length);
}, 1000);
const dataLength = largeData.length;
setInterval(() => {
console.log(dataLength);
}, 1000);
Event Listener Cleanup
emitter.on("data", handler);
emitter.once("data", handler);
emitter.on("data", handler);
emitter.removeListener("data", handler);
AbortSignal/AbortController
const controller = new AbortController();
const response = await fetch(url, { signal: controller.signal });
setTimeout(() => controller.abort(), 30000);
Module-Level Variables
const cache: Map<string, Data> = new Map();
export function getData(key: string) {
if (!cache.has(key)) {
cache.set(key, expensiveCompute(key));
}
return cache.get(key);
}
import { LRUCache } from "lru-cache";
const cache = new LRUCache<string, Data>({ max: 1000 });
Debugging Workflow
Memory Leak Investigation
- Baseline: Create heap snapshot at startup
- Reproduce: Perform suspect operations
- Compare: Create second snapshot, compare in DevTools
- Identify: Look for growing object counts (Delta column)
- Trace: Use retainers view to find what's holding references
Performance Investigation
- Profile: Run with
--cpu-prof
- Load: Open
.cpuprofile in Chrome DevTools
- Analyze: Check flame graph for hot paths
- Optimize: Focus on widest flames first
Agentic Optimizations
| Context | Command |
|---|
| Quick debug | bun --inspect-brk script.ts |
| Debug tests | bun --inspect-brk test |
| Memory check | bun -e "import{heapStats}from'bun:jsc';console.log(heapStats())" |
| Network debug | BUN_CONFIG_VERBOSE_FETCH=curl bun script.ts |
| CPU profile | bun --cpu-prof script.ts |
| Native memory | MIMALLOC_SHOW_STATS=1 bun script.ts |
Quick Reference
Inspector Flags
| Flag | Description |
|---|
--inspect | Enable debugger on available port |
--inspect=<port> | Enable debugger on specific port |
--inspect-brk | Break at first line |
--inspect-wait | Wait for debugger attachment |
--cpu-prof | Generate CPU profile |
Debug URLs
| URL | Purpose |
|---|
debug.bun.sh | Bun's web debugger |
chrome://inspect | Chrome DevTools (for heap analysis) |
Environment Variables
| Variable | Description |
|---|
BUN_CONFIG_VERBOSE_FETCH | 1 or curl for request logging |
MIMALLOC_SHOW_STATS | 1 to show native memory stats |
Memory APIs
| API | Import | Purpose |
|---|
writeHeapSnapshot() | v8 | Create heap snapshot file |
heapStats() | bun:jsc | Get heap statistics |
memoryUsage() | process | Get process memory |
memoryUsage.rss() | process | Get resident set size |
Troubleshooting
Debugger Not Connecting
lsof -i :6499
bun --inspect=9229 script.ts
Breakpoints Not Hit
- Ensure sourcemaps are enabled
- Use
--inspect-brk for fast-exiting scripts
- Check file paths match in debugger
VSCode Issues (Windows)
Bun's Unix socket debugging may not work on Windows. Use WSL or the web debugger instead.