{"record":{"id":"b8b947d2e732385d","repo":"sveltejs/svelte","slug":"effect-orphan","errorCode":"effect_orphan","errorMessage":"effect_orphan\n`${rune}` can only be used inside an effect (e.g. during component initialisation)\nhttps://svelte.dev/e/effect_orphan","messagePattern":"effect_orphan\n`(.+?)` can only be used inside an effect \\(e\\.g\\. during component initialisation\\)\nhttps://svelte\\.dev/e/effect_orphan","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":215,"sourceCode":"\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}\n}\n\n/**\n * `$effect.pending()` can only be called inside an effect or derived\n * @returns {never}\n */\nexport function effect_pending_outside_reaction() {\n\tif (DEV) {\n\t\tconst error = new Error(`effect_pending_outside_reaction\\n\\`$effect.pending()\\` can only be called inside an effect or derived\\nhttps://svelte.dev/e/effect_pending_outside_reaction`);\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_pending_outside_reaction`);\n\t}\n}","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L197-L233","documentation":"Thrown by validate_effect() in reactivity/effects.js:55 when $effect/$effect.pre/$inspect is called with both active_effect and active_reaction equal to null — i.e. completely outside any reactive context. Svelte's effect runes require a parent effect (normally the component init effect) so they can be tracked and torn down; with no active reaction at all there is nothing to own them.","triggerScenarios":"Calling $effect(...), $effect.pre(...), or $inspect(...) at module top-level, inside a plain async function with no component running, in an event handler registered outside component init, or in a setTimeout/setImmediate callback that runs after component init finished. The guard fires at effects.js:52-59 because active_effect===null and active_reaction===null.","commonSituations":"Calling $effect inside a setTimeout in module scope; invoking a runes-mode .svelte.js helper from plain Node code; refactoring onMount logic into an external function and forgetting it must be called during component init; testing runes code without mounting a component.","solutions":["Ensure $effect/$inspect is called during component initialisation (top level of a .svelte <script> or a function invoked synchronously from it).","If you need an effect outside a component, use $effect.root(() => { ... }) which creates an explicit root owner.","Move the side effect into an event handler or onMount rather than a bare rune call."],"exampleFix":"// before — module.svelte.js\nexport function setup() {\n  $effect(() => { console.log('runs nowhere'); });\n}\nsetup(); // called from plain JS\n\n// after — call from a component\n// Component.svelte\n<script>\n  import { setup } from './module.svelte.js';\n  setup(); // now has an active effect\n</script>","handlingStrategy":"validation","validationCode":"// Ensure $effect is only called during component init.\n// Svelte exposes no public 'is inside effect' check; the discipline is structural:\n// only call $effect/$effect.pre/$inspect from the top level of a .svelte <script>\n// or from a function synchronously invoked there.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call $effect from setTimeout/setInterval/then callbacks unless wrapped in $effect.root.","Keep runes calls inside component initialisation or .svelte.js functions invoked from a component.","For effects that must outlive a component, use $effect.root and store the returned cleanup function."],"tags":["svelte","runes","effects","reactivity","lifecycle"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}