-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Component fails to mount with error Uncaught TypeError: Cannot read properties of undefined (reading 'context') #7847
Comments
The reason that it is working fine in dev mode is because the program is terminating here for the FirstComponent: svelte/src/runtime/internal/dev.ts Line 169 in 24aff30
But in prod build, this is removed and the FirstComponent init is attempted which sets the svelte/src/runtime/internal/Component.ts Line 109 in 24aff30
Next time when SecondComponent is initialised, svelte/src/runtime/internal/Component.ts Line 108 in 24aff30
But the svelte/src/runtime/internal/Component.ts Line 127 in 24aff30
|
Any progress or workaround? |
This is intentional. function mountFirstComponent() {
const target = document.querySelector('#first-field');
if (!target) throw Error('target is not exists!')
new First({target: document.querySelector('#first-field')});
} |
@baseballyama How do i implement you solution into a bog-standard svelte app cuz im also getting this error. Here's the entry point main.js file: import './style.css'
import App from './App.svelte'
const app = new App({
target: document.getElementById('app'),
})
export default app Note that I'm not using ANY extra libraries, just svelte, vite and vite-plugin-svelte. |
I think HTML doesn't have |
Although this issue was closed from dev mode perspective, but the issue is present in prod build as well. In dev mode it early exits from the error, but in prod build it fails to mount the component and any component after that. I believe this should be internally handled by svelte where it should not fail to mount the second component (even if target was present in the DOM) when first component fails to mount (because target was not present in DOM). |
For future reference, I got this error when enableSourcemap value was set to
In this setup DEV_MODE was not present in .env, thus leading to this difficult to debug error. Silly me |
Describe the bug
Note: Issue only reproducible with prod build
Bug introduced in version:
3.40.0
Trying to mount first component with a target that is not present in DOM. So, this should fail to mount and throw an error (and this behaviour is correct).
Ex: An element with id
#first-field
is not present in the DOM. And we try to mount the component like this:this fails with error Uncaught TypeError: Cannot read properties of undefined (reading '$$') (this is correct and expected)
But when we try to mount component with a valid target, right after the first component, this second component fails to mount too.
Ex: An element with id
#second-field
is present in the DOM. And we try to mount the component like this:this fails with error This fails to load too with error Uncaught TypeError: Cannot read properties of undefined (reading 'context') (this is not expected)
Reproduction
Clone: https://github.com/sohaibalam67/svelte-bug-component-mount
run
npm run build && npm run start
Click on 'mount component without target' button
this fails with error
Uncaught TypeError: Cannot read properties of undefined (reading '$$')
Click on 'mount component with target' button
This fails to load too with error
Uncaught TypeError: Cannot read properties of undefined (reading 'context')
The issue is only reproducible in prod build and not in dev build
Logs
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: