sveltejs/svelte · error · Error
missing_context
missing_context
Error message
missing_context Context was not set in a parent component https://svelte.dev/e/missing_context
What it means
`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.
Source
Thrown at packages/svelte/src/internal/shared/errors.js:100
throw error;
} else {
throw new Error(`https://svelte.dev/e/lifecycle_outside_component`);
}
}
/**
* Context was not set in a parent component
* @returns {never}
*/
export function missing_context() {
if (DEV) {
const error = new Error(`missing_context\nContext was not set in a parent component\nhttps://svelte.dev/e/missing_context`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/missing_context`);
}
}
/**
* 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()}`.
* @returns {never}
*/
export function snippet_without_render_tag() {
if (DEV) {
const 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`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/snippet_without_render_tag`);
}
}View on GitHub (pinned to 20b341f100)
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`.
Example fix
// before
const theme = getContext('theme'); // no ancestor set it -> throws
// after
// in a parent layout:
setContext('theme', 'dark');
// in the child:
const theme = getContext('theme') ?? 'light'; Defensive patterns
Strategy: validation
Validate before calling
import { hasContext, getContext } from 'svelte';
const theme = hasContext('theme') ? getContext('theme') : 'light'; Type guard
import { hasContext, getContext } from 'svelte';
function requireContext(key, fallback) {
return hasContext(key) ? getContext(key) : fallback;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Forgetting `setContext` in a layout/wrapper component; key typos; conditionally setting context that a child always reads; refactoring a provider out of the tree.
Related errors
AI-assisted analysis of sveltejs/svelte@20b341f100 (2026-08-12).
Data as JSON: /api/errors/5fac49f55d44bd1e.
Report an issue: GitHub.