{"record":{"id":"9728b58ecfe9e864","repo":"sveltejs/svelte","slug":"effect-in-teardown","errorCode":"effect_in_teardown","errorMessage":"effect_in_teardown\n`${rune}` cannot be used inside an effect cleanup function\nhttps://svelte.dev/e/effect_in_teardown","messagePattern":"effect_in_teardown\n`(.+?)` cannot be used inside an effect cleanup function\nhttps://svelte\\.dev/e/effect_in_teardown","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":182,"sourceCode":"\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/each_key_volatile`);\n\t}\n}\n\n/**\n * `%rune%` cannot be used inside an effect cleanup function\n * @param {string} rune\n * @returns {never}\n */\nexport function effect_in_teardown(rune) {\n\tif (DEV) {\n\t\tconst error = new Error(`effect_in_teardown\\n\\`${rune}\\` cannot be used inside an effect cleanup function\\nhttps://svelte.dev/e/effect_in_teardown`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/effect_in_teardown`);\n\t}\n}\n\n/**\n * Effect cannot be created inside a `$derived` value that was not itself created inside an effect\n * @returns {never}\n */\nexport function effect_in_unowned_derived() {\n\tif (DEV) {\n\t\tconst error = new Error(`effect_in_unowned_derived\\nEffect cannot be created inside a \\`$derived\\` value that was not itself created inside an effect\\nhttps://svelte.dev/e/effect_in_unowned_derived`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/effect_in_unowned_derived`);\n\t}\n}","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L164-L200","documentation":"Runtime error `effect_in_teardown`: a Svelte 5 rune (`$effect`, `$pre_effect`, or `$derived`) was used inside an effect cleanup (teardown) function. Effects and deriveds must be created during the setup phase of an owner effect, not during cleanup, because teardowns run in an unowned context. The message names the offending rune.","triggerScenarios":"At runtime (DEV) when the body of an `$effect` cleanup callback (the function returned from the effect, or registered via `$.onDestroy`-style APIs) calls `$effect(...)`, `$derived(...)`, or another lifecycle rune.","commonSituations":"Migrating Svelte 4 `onDestroy(() => { ... })` callbacks that set up new reactive computations; cleanup functions that try to subscribe to a store or start a timer-driven effect; refactoring that moved setup logic into teardown by mistake.","solutions":["Move the `$effect`/`$derived` creation out of the cleanup and into the effect body (the setup phase).","For cleanup-only work, do plain imperative teardown (clear timers, remove listeners) without using runes.","If you need a derived that lives across the effect lifetime, declare it at component scope, not inside the cleanup."],"exampleFix":"// before\n$effect(() => {\n  return () => {\n    $effect(() => { /* ... */ }); // teardown starting an effect\n  };\n});\n\n// after\n$effect(() => {\n  // setup runs here\n  $effect(() => { /* ... */ });\n  return () => {\n    // cleanup: only plain imperative teardown, no runes\n  };\n});","handlingStrategy":"validation","validationCode":"// Detect rune usage inside an $effect cleanup return.\nfunction runeInTeardown(source) {\n  // crude: finds `$effect(() => { ... return () => { $effect(...)/$derived(...) } })`\n  return /return\\s*\\(\\s*\\(?\\)\\s*=>\\s*\\{[\\s\\S]*?\\$(?:effect|derived|pre_effect)\\(/.test(source);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create runes only in the setup (body) of an `$effect`, never in its returned cleanup.","Keep teardown callbacks imperative: clear timers, remove listeners, abort fetches.","Declare cross-effect deriveds at component scope, not inside cleanup closures."],"tags":["svelte5","runtime","effects","runes","lifecycle","cleanup"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}