Svelte DevTools
The official browser extension provides powerful debugging capabilities:Installation
- Chrome/Edge: Svelte DevTools on Chrome Web Store
- Firefox: Svelte DevTools on Firefox Add-ons
Features
Component Tree:- View component hierarchy
- Inspect component props and state
- See which components are mounted
- Track component parent-child relationships
- View all reactive state (
$state) - Inspect derived values (
$derived) - Monitor state changes in real-time
- Modify state values directly
- Track component lifecycle events
- See when effects run
- Monitor prop updates
The $inspect Rune
Basic Usage
Log reactive state changes with$inspect:
$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:
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:
firstName changes:
Browser DevTools
Chrome DevTools
Elements Panel:- Inspect DOM structure
- View component-generated HTML
- Modify styles in real-time
- Check element accessibility
1
- Monitor API requests
- Check request/response times
- Debug failed requests
- Inspect headers and payloads
Source Maps
Svelte generates source maps automatically in development:- Set breakpoints in
.sveltefiles - 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$state
$inspect.trace:
Props Not Updating
Problem: Child component doesn’t react to prop changes$derived for computed props
Infinite Loops in Effects
Problem: Effect triggers itselfComponent Not Rendering
Problem: Component instance doesn’t appear in DOM Check:1
{#if} block?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
$inspect liberally - It’s removed in production automaticallydebugger statements - Pause execution at specific points