{"record":{"id":"794baeee2ec77f1a","repo":"sveltejs/svelte","slug":"lifecycle-outside-component","errorCode":"lifecycle_outside_component","errorMessage":"lifecycle_outside_component\n`${name}(...)` can only be used during component initialisation\nhttps://svelte.dev/e/lifecycle_outside_component","messagePattern":"lifecycle_outside_component\n`(.+?)\\(\\.\\.\\.\\)` can only be used during component initialisation\nhttps://svelte\\.dev/e/lifecycle_outside_component","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/shared/errors.js","lineNumber":84,"sourceCode":"\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/invariant_violation`);\n\t}\n}\n\n/**\n * `%name%(...)` can only be used during component initialisation\n * @param {string} name\n * @returns {never}\n */\nexport function lifecycle_outside_component(name) {\n\tif (DEV) {\n\t\tconst error = new Error(`lifecycle_outside_component\\n\\`${name}(...)\\` can only be used during component initialisation\\nhttps://svelte.dev/e/lifecycle_outside_component`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/lifecycle_outside_component`);\n\t}\n}\n\n/**\n * Context was not set in a parent component\n * @returns {never}\n */\nexport function missing_context() {\n\tif (DEV) {\n\t\tconst error = new Error(`missing_context\\nContext was not set in a parent component\\nhttps://svelte.dev/e/missing_context`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/missing_context`);\n\t}\n}","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/shared/errors.js#L66-L102","documentation":"Svelte lifecycle functions (`onMount`, `onDestroy`, `beforeUpdate`, `afterUpdate`, and rune-based equivalents) only work during component initialization — the synchronous setup phase when the component function runs. Calling them later throws because there is no active component context. The `%name%` identifies which lifecycle function was misused.","triggerScenarios":"Calling `onMount(...)` inside an `async` function after an `await` (context is lost across the boundary); calling a lifecycle in an event handler or `setTimeout` callback; calling it from a plain `.js`/`.ts` module outside any component.","commonSituations":"Top-level `await` in component init that pushes lifecycle calls past the await; calling `onMount` from a utility not always invoked during init; refactoring init code into async helpers.","solutions":["Register lifecycle functions synchronously at the top of component init, before any `await`.","Do async work inside the lifecycle callback: `onMount(async () => { await ... })`.","Move lifecycle registration out of event handlers and timeouts into the component body."],"exampleFix":"// before\nlet data;\nasync function init() {\n\tawait load();\n\tonMount(() => console.log('mounted')); // throws: past await, no component context\n}\n// after\nonMount(async () => {\n\tawait load();\n\tconsole.log('mounted');\n});","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call lifecycle functions synchronously at the top of component init, before any `await`.","Put `await` inside the lifecycle callback: `onMount(async () => { ... })`.","Never call lifecycle functions from event handlers, timeouts, or post-await code.","Keep init logic synchronous; defer async work into the lifecycle callback."],"tags":["lifecycle","component-init","async","runes","svelte5"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}