Cannot read property 'source' of undefined when importing a missing Svelte component
0 reputation · 06 Jun 2022, 06:23 UTC
Compiler Error Context
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.
Unresolved Diagnostic Decision
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.
Key Trade‑offs
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.
Questions for Discussion
- Should the Svelte compiler emit distinct error messages for missing imports versus syntax errors in imported components?
- What is the minimal change required to the compiler’s AST handling to support such fine‑grained diagnostics?
- How can the compiler balance improved error specificity against the risk of increased compile times?