---
name: debug:svelte
description: Debug Svelte application issues systematically. This skill helps diagnose and resolve Svelte-specific problems including reactivity failures, runes issues ($state, $derived, $effect), store subscription memory leaks, SSR hydration mismatches, and compiler warnings. Covers both Svelte 4 legacy patterns and Svelte 5 runes-based reactivity. Provides debugging tools like $inspect(), {@debug} tags, and svelte-check CLI usage.
---
# Svelte Debugging Guide
This guide provides a systematic approach to debugging Svelte applications, with special emphasis on Svelte 5 runes and reactivity patterns.
## Common Error Patterns
### 1. Reactivity Not Triggering
**Symptoms:**
- UI doesn't update when state changes
- Computed values are stale
- Event handlers seem to fire but nothing happens
**Root Causes:**
```svelte
```
**Svelte 5 Fix with Runes:**
```svelte
```
### 2. Runes ($state, $derived, $effect) Issues
**Error: "Cannot use runes in .js files"**
```bash
# WRONG: Using runes in regular JS file
# utils.js
export const count = $state(0); // ERROR!
# CORRECT: Rename to .svelte.js
# utils.svelte.js
export const count = $state(0); // Works!
```
**Error: "Class properties not reactive"**
```javascript
// WRONG: $state wrapper on class instance has no effect
let instance = $state(new MyClass()); // Properties NOT reactive
// CORRECT: Make class properties reactive internally
class MyClass {
count = $state(0);
name = $state('');
}
let instance = new MyClass(); // Properties ARE reactive
```
**Error: "Infinite loop in $derived"**
```svelte
```
**Error: "Infinite update loop in $effect"**
```svelte
```
**Error: "Cannot export reassigned $state"**
```javascript
// WRONG: Exporting reassigned state
// store.svelte.js
export let count = $state(0); // ERROR if reassigned
// CORRECT: Wrap in object for mutation
export const state = $state({ count: 0 });
// Or use getter/setter
let _count = $state(0);
export const count = {
get value() { return _count; },
set value(v) { _count = v; }
};
```
### 3. Store Subscription Problems (Svelte 4)
**Memory Leak: Forgotten Unsubscribe**
```svelte
```
**Store Not Updating**
```javascript
// WRONG: Mutating store value
import { writable } from 'svelte/store';
const items = writable([]);
items.update(arr => {
arr.push('new item'); // Mutation, may not trigger
return arr;
});
// CORRECT: Return new array
items.update(arr => [...arr, 'new item']);
```
### 4. SSR Hydration Mismatches
**Symptoms:**
- Console warning about hydration mismatch
- Content flickers on page load
- Interactive elements don't work initially
**Common Causes:**
```svelte
```
**LocalStorage Access**
```svelte
```
### 5. Compiler Errors
**"'foo' is not defined"**
```svelte
```
**"Cannot bind to property"**
```svelte
```
## Debugging Tools
### 1. Svelte DevTools Browser Extension
Install from Chrome Web Store or Firefox Add-ons. Provides:
- Component tree visualization
- Props and state inspection
- Store value monitoring
- Event tracking
### 2. The $inspect() Rune (Svelte 5)
```svelte
```
### 3. The {@debug} Tag
```svelte
{@debug items, filter}
{#each items as item}
{@debug item}
{item}
{/each}
```
### 4. Console Methods
```svelte
```
### 5. Vite Dev Server
```bash
# Start with verbose logging
npm run dev -- --debug
# Clear cache if issues persist
rm -rf node_modules/.vite
npm run dev
```
### 6. svelte-check CLI
```bash
# Type checking and diagnostics
npx svelte-check
# Watch mode
npx svelte-check --watch
# With specific tsconfig
npx svelte-check --tsconfig ./tsconfig.json
# Output format for CI
npx svelte-check --output human-verbose
```
### 7. VS Code Debugging
```json
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Debug Svelte",
"url": "http://localhost:5173",
"webRoot": "${workspaceFolder}",
"sourceMaps": true
}
]
}
```
Enable breakpoints in .svelte files:
1. Open VS Code settings
2. Search for `debug.allowBreakpointsEverywhere`
3. Enable the setting
## The Four Phases (Svelte-specific)
### Phase 1: Gather Information
```bash
# Check Svelte version
npm list svelte
# Check for type errors
npx svelte-check --output human-verbose
# Check browser console for warnings/errors
# Look for:
# - Hydration warnings
# - Reactivity warnings
# - Unhandled promise rejections
# Check component props
$inspect($$props); # Svelte 5
```
**Questions to Answer:**
- Is this a Svelte 4 or Svelte 5 project?
- Are you using SvelteKit or standalone Svelte?
- Does the issue occur in dev, build, or both?
- Is it SSR-related (works in CSR but not SSR)?
### Phase 2: Isolate the Problem
```svelte
```
**Isolation Strategies:**
1. Comment out unrelated code
2. Remove external dependencies
3. Test in fresh component
4. Check if issue is component-specific or global
### Phase 3: Form Hypothesis and Test
**Common Hypotheses:**
| Symptom | Hypothesis | Test |
|---------|------------|------|
| No reactivity | Missing $state | Add $state wrapper |
| Infinite loop | Circular dependency | Check $derived/$effect |
| SSR error | Browser API in SSR | Add browser guard |
| Props not reactive | Destructured props | Use $props() correctly |
| Store stale | Subscription issue | Use auto-subscription $ |
```svelte
```
### Phase 4: Fix and Verify
**Verification Checklist:**
- [ ] Issue no longer occurs
- [ ] No new console warnings
- [ ] svelte-check passes
- [ ] Works in both dev and build
- [ ] Works in SSR (if applicable)
- [ ] No memory leaks (check DevTools)
```bash
# Full verification
npx svelte-check
npm run build
npm run preview # Test production build
```
## Quick Reference Commands
### Diagnostic Commands
```bash
# Check Svelte/SvelteKit versions
npm list svelte @sveltejs/kit
# Run type checking
npx svelte-check
# Run type checking with watch
npx svelte-check --watch
# Check for outdated packages
npm outdated
# Clear caches
rm -rf node_modules/.vite .svelte-kit
npm run dev
```
### Common Fixes
```bash
# Reset node_modules
rm -rf node_modules package-lock.json
npm install
# Regenerate SvelteKit types
npx svelte-kit sync
# Update Svelte to latest
npm update svelte @sveltejs/kit
# Check for peer dependency issues
npm ls
```
### Debug Environment Variables
```bash
# Enable Vite debug mode
DEBUG=vite:* npm run dev
# Enable SvelteKit debug mode
DEBUG=kit:* npm run dev
# Both
DEBUG=vite:*,kit:* npm run dev
```
## Svelte 5 Migration Pitfalls
### From let to $state
```svelte
```
### From $: to $derived/$effect
```svelte
```
### From export let to $props
```svelte
```
### From createEventDispatcher to Callback Props
```svelte
```
## Reactive Collections (Svelte 5)
```javascript
// Import reactive versions of Map, Set, etc.
import { SvelteMap, SvelteSet, SvelteURL } from 'svelte/reactivity';
// Use instead of native collections
let items = new SvelteMap();
let tags = new SvelteSet();
// These are deeply reactive
items.set('key', { nested: 'value' });
tags.add('new-tag');
```
## Form Handling with tick()
```svelte
```
## Sources
- [Svelte @debug Documentation](https://svelte.dev/docs/svelte/@debug)
- [SvelteKit Breakpoint Debugging](https://svelte.dev/docs/kit/debugging)
- [Svelte 5 Migration Guide](https://svelte.dev/docs/svelte/v5-migration-guide)
- [Svelte 5 States: Avoiding Common Reactivity Traps](https://jamesy.dev/blog/svelte-5-states-avoiding-common-reactivity-traps)
- [The Guide to Svelte Runes](https://sveltekit.io/blog/runes)
- [Introducing Runes](https://svelte.dev/blog/runes)
- [Exploring the Magic of Runes in Svelte 5](https://blog.logrocket.com/exploring-runes-svelte-5/)