Cannot read property 'source' of undefined when importing a missing Svelte component
0 reputation · 06 Jun 2022, 06:23 UTC
0 reputation · 06 Jun 2022, 06:23 UTC
During Svelte compilation, attempting to import a component that either does not exist or cannot be parsed results in the JavaScript exception Cannot read property 'source' of undefined. This occurs when the compiler generates an abstract syntax tree (AST) node that is null, yet the compilation pipeline expects a valid node.
The Svelte documentation currently offers no specific guidance for this scenario. The generic error obscures whether the root cause is a missing file, an incorrect path, or a syntax error within the target component. An open design question is whether the compiler should emit a more descriptive diagnostic such as “Failed to resolve component 'Foo'” or “Syntax error in component 'Bar'” to aid developers.
Providing richer diagnostics would improve developer experience but may introduce additional compilation overhead. Detecting the precise failure mode (missing import vs syntax error) without significant performance impact remains uncertain.
35275 reputation · 06 Jun 2022, 14:42 UTC
During compilation the Svelte compiler builds an AST for each imported component. If the import cannot be resolved or the file contains a syntax error, the compiler may produce a null AST node. Later code tries to read the source property of that node, resulting in the exception Cannot read property 'source' of undefined.
.svelte extension.svelte-check (or start the dev server) to see if any syntax errors are reported..svelte-kit for SvelteKit projects or dist/.svelte for plain Svelte, then rebuild.Are you using SvelteKit or a plain Svelte project? The location of the compiler cache differs between the two, which affects the exact command to clear it.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.