Skip to main content
Svelte provides excellent debugging tools and techniques to help you identify and fix issues in your applications.

Svelte DevTools

The official browser extension provides powerful debugging capabilities:

Installation

Features

Component Tree:
  • View component hierarchy
  • Inspect component props and state
  • See which components are mounted
  • Track component parent-child relationships
State Inspector:
  • View all reactive state ($state)
  • Inspect derived values ($derived)
  • Monitor state changes in real-time
  • Modify state values directly
Event Timeline:
  • Track component lifecycle events
  • See when effects run
  • Monitor prop updates
Enable Svelte DevTools in development mode for the best debugging experience. It automatically detects Svelte applications.

The $inspect Rune

Basic Usage

Log reactive state changes with $inspect:
Output:
$inspect only works in development mode. It becomes a no-op in production builds.

Deep Reactivity Tracking

$inspect tracks changes deeply in objects and arrays:

Custom Logging with .with

Provide your own logging function:
Parameters:
  • type: "init" (first call) or "update" (subsequent changes)
  • ...values: The values passed to $inspect

Tracing Reactive Dependencies

Use $inspect.trace() to debug why effects or derived values re-run:
Console output when firstName changes:
$inspect.trace() must be the first statement in an effect or derived function body.

Browser DevTools

Chrome DevTools

Elements Panel:
  • Inspect DOM structure
  • View component-generated HTML
  • Modify styles in real-time
  • Check element accessibility
Console:
Now in console:
Performance Panel:
1
  • Start recording
  • Interact with your app (click, scroll, type)
  • Stop recording
  • Analyze flame graph for slow operations
  • Identify expensive component renders
  • Network Panel:
    • Monitor API requests
    • Check request/response times
    • Debug failed requests
    • Inspect headers and payloads

    Source Maps

    Svelte generates source maps automatically in development:
    With source maps, you can:
    • Set breakpoints in .svelte files
    • Step through original source code
    • See original variable names
    • Get accurate stack traces

    Common Debugging Scenarios

    Reactivity Not Working

    Problem: Component doesn’t update when state changes
    Solution: Use $state
    Debug with $inspect.trace:

    Props Not Updating

    Problem: Child component doesn’t react to prop changes
    Solution: Use $derived for computed props

    Infinite Loops in Effects

    Problem: Effect triggers itself
    Solution: Use the right tool
    Debug infinite loops:

    Component Not Rendering

    Problem: Component instance doesn’t appear in DOM Check:
    1
  • Import path: Is the component imported correctly?
  • Component name: Does it start with a capital letter?
  • Conditional rendering: Is it inside a falsy {#if} block?
  • Parent mount: Is the parent component mounted?
  • Error Boundaries

    Catch and handle component errors gracefully:
    Use <svelte:boundary> to prevent errors from crashing your entire app.

    Testing and Debugging

    Component Tests with Vitest

    Write tests that act as debugging documentation:

    Debug Mode in Tests

    Logging Best Practices

    Structured Logging

    Conditional Logging

    Development vs Production

    Environment Detection

    Debug Panels

    Debugging Tips

    1
  • Use $inspect liberally - It’s removed in production automatically
  • Enable Svelte DevTools - Essential for understanding component state
  • Add debugger statements - Pause execution at specific points
  • Log component lifecycle - Track when components mount/unmount
  • Test edge cases - Write tests for problematic scenarios
  • Use TypeScript - Catch type errors before runtime
  • Check the compiler warnings - Svelte warns about common mistakes
  • Profile performance - Use browser tools to find bottlenecks
  • Common mistakes to avoid:
    • Forgetting to use $state for reactive variables
    • Using $effect instead of $derived for computed values
    • Not providing keys in {#each} blocks
    • Modifying props directly (use callbacks or events)
    • Ignoring compiler warnings

    Additional Resources

    The best debugging tool is prevention. Use TypeScript, write tests, and pay attention to compiler warnings to catch issues early.