{"record":{"id":"5fac49f55d44bd1e","repo":"sveltejs/svelte","slug":"missing-context","errorCode":"missing_context","errorMessage":"missing_context\nContext was not set in a parent component\nhttps://svelte.dev/e/missing_context","messagePattern":"missing_context\nContext was not set in a parent component\nhttps://svelte\\.dev/e/missing_context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/shared/errors.js","lineNumber":100,"sourceCode":"\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}\n\n/**\n * Attempted to render a snippet without a `{@render}` block. This would cause the snippet code to be stringified instead of its content being rendered to the DOM. To fix this, change `{snippet}` to `{@render snippet()}`.\n * @returns {never}\n */\nexport function snippet_without_render_tag() {\n\tif (DEV) {\n\t\tconst error = new Error(`snippet_without_render_tag\\nAttempted to render a snippet without a \\`{@render}\\` block. This would cause the snippet code to be stringified instead of its content being rendered to the DOM. To fix this, change \\`{snippet}\\` to \\`{@render snippet()}\\`.\\nhttps://svelte.dev/e/snippet_without_render_tag`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/snippet_without_render_tag`);\n\t}\n}","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/shared/errors.js#L82-L118","documentation":"`getContext(key)` retrieves a value an ancestor component set via `setContext(key, value)`. If no ancestor set that key, the context lookup fails and Svelte throws in DEV. Context is hierarchical and must be set during a parent's initialization before children read it.","triggerScenarios":"Calling `getContext('theme')` when no ancestor called `setContext('theme', ...)`; using a different key object (context keys are compared by identity, so a string literal vs a Symbol mismatch); reading context outside any component tree.","commonSituations":"Forgetting `setContext` in a layout/wrapper component; key typos; conditionally setting context that a child always reads; refactoring a provider out of the tree.","solutions":["Ensure an ancestor component calls `setContext(key, value)` before the reading component mounts.","Use a shared, stable key (e.g. an exported `Symbol()`) to avoid identity/typo mismatches.","Provide a fallback for optional context: `getContext(key) ?? defaultValue`."],"exampleFix":"// before\nconst theme = getContext('theme'); // no ancestor set it -> throws\n// after\n// in a parent layout:\nsetContext('theme', 'dark');\n// in the child:\nconst theme = getContext('theme') ?? 'light';","handlingStrategy":"validation","validationCode":"import { hasContext, getContext } from 'svelte';\nconst theme = hasContext('theme') ? getContext('theme') : 'light';","typeGuard":"import { hasContext, getContext } from 'svelte';\nfunction requireContext(key, fallback) {\n\treturn hasContext(key) ? getContext(key) : fallback;\n}","tryCatchPattern":null,"preventionTips":["Use `hasContext(key)` before `getContext(key)` for optional values.","Provide a default: `getContext(k) ?? default`.","Keep context keys as exported Symbols to avoid typos and identity mismatches.","Set context in the nearest common ancestor before children mount."],"tags":["context","getcontext","setcontext","component-tree"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}