{"record":{"id":"1ef9aab99ad01fbc","repo":"sveltejs/svelte","slug":"effect-in-unowned-derived","errorCode":"effect_in_unowned_derived","errorMessage":"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","messagePattern":"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","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":198,"sourceCode":"\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}\n\n/**\n * `%rune%` can only be used inside an effect (e.g. during component initialisation)\n * @param {string} rune\n * @returns {never}\n */\nexport function effect_orphan(rune) {\n\tif (DEV) {\n\t\tconst error = new Error(`effect_orphan\\n\\`${rune}\\` can only be used inside an effect (e.g. during component initialisation)\\nhttps://svelte.dev/e/effect_orphan`);\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_orphan`);\n\t}","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L180-L216","documentation":"Thrown by validate_effect() in reactivity/effects.js:58 when $effect/$effect.pre/$inspect is invoked while active_effect is null but active_reaction is non-null. This means the call is happening inside a $derived whose chain does not trace back to an owning effect (an 'unowned' derived, e.g. a top-level module-scope $derived). Svelte forbids this because effects created without an owner can never be cleaned up and would leak or run detached from any component lifecycle.","triggerScenarios":"Calling $effect(() => {...}) inside the body of a $derived(...) that was itself created outside any component/effect (module scope, or a plain .svelte.js function not called from a component). The validate_effect() guard at effects.js:52-59 fires because active_effect===null (no owner) yet active_reaction!==null (we are inside the derived's evaluation).","commonSituations":"Creating a top-level `const x = $derived(() => { $effect(() => {...}) })` in a .svelte.js module; refactoring logic out of a component into a derived helper that internally tries to register a side effect; mixing $effect into derived-by-callable patterns during Svelte 5 runes migration.","solutions":["Move the $effect out of the $derived and into a component body or $effect.root so it has a real owner.","Replace the effect-inside-derived with pure derivation — compute the value, perform side effects in a separate $effect that reads the derived.","If you truly need an effect with no component owner, wrap it in $effect.root(...) and manage teardown explicitly."],"exampleFix":"// before\nexport const data = $derived.by(() => {\n  $effect(() => { console.log('side effect') });\n  return fetchValue();\n});\n\n// after — keep the derived pure, run the effect in a component\nexport const data = $derived.by(() => fetchValue());\n// in a .svelte component:\n$effect(() => { console.log('side effect', data); });","handlingStrategy":"validation","validationCode":"// Before calling $effect inside a derived, ensure the derived is owned by an effect.\n// In Svelte there is no public API to check ownership at runtime; instead validate structurally:\n// Never place $effect inside $derived.by/$derived unless the derived is created within a component.\n// Refactor: keep deriveds pure.\nfunction safeEffectInDerived(getter) {\n  // $effect must be called from a component or $effect.root, not from a derived.\n  throw new Error('Move the $effect out of the $derived; deriveds must be pure.');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat $derived bodies as pure functions — no $effect, no side effects, no state writes.","Run side effects in a separate $effect that reads the derived value.","When extracting logic from a component into a .svelte.js helper, keep effects in the component or wrap in $effect.root."],"tags":["svelte","runes","effects","derived","reactivity"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}