Fix: Show helpful error when using new App()
syntax in Svelte 5
#16700
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
In Svelte 5, components are no longer classes and should be instantiated using
mount()
instead ofnew Component()
. However, when users try the old syntax, they get a cryptic error instead of helpful guidance:The cryptic "nodes_start" error provides no indication that the issue is using the wrong instantiation method, making migration from Svelte 4 to 5 unnecessarily difficult.
Root Cause
Svelte 5 components include a
check_target(new.target)
call to detect constructor usage and show a helpful error message. However, this check was only included in development builds (dev: true
), not in production builds. When the check is missing, the component execution continues with invalid parameters, eventually failing deep in the DOM manipulation code with the cryptic error.Solution
Modified the compiler to always include the constructor check in both development and production builds. The helpful error message is already implemented and working correctly - it just needed to be enabled in production.
Before (production build):
After (all builds):
Changes
transform-client.js
from checkingdev
mode only to always including the constructor check (except when using legacy compatibility mode)The change is minimal and surgical - only 2 lines modified in the compiler, with the existing error handling infrastructure doing the rest.
Impact
Fixes the issue described in the problem statement where users encounter confusing errors when using the old component instantiation syntax.
Related Issue
#13991