vite-plugin-svelte: Cannot read properties of undefined (reading 'startsWith')
Describe the bug
Error on use Typescript type. All explications in “reproduction” field.
Using:
- SvelteKit
- Svelte 5
- Vite 6
- Selected Typescript in setup
Reproduction URL
Reproduction
How to reproduce the bug:
- Create a new Sveltekit project, with typescript
- Install deps and run dev
- in +page.svelte, create a typed var:
const test: string = "abc";
How to be sure that the bug comes from there:
4. Remove the type: const test = "abc";
5. The bug disappears
Logs
No response
System Info
at couldBeFixedByCssPreprocessor (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:111:72)
at enhanceCompileError (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:126:6)
at compileSvelte (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/compile.js:148:4)
at async handleHotUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/handle-hot-update.js:33:17)
at async handleHMRUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:44193:33)
at async onHMRUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:43585:7)
at async FSWatcher.<anonymous> (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:43622:5
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 21 (5 by maintainers)
The bug isn’t in
esrap, it’s insvelte— we’re handingesrapa malformed AST. Working on itfixed in 5.14.3
Adding some context here.
Having this
+page.svelte:Gives the error that OP reported:
Diving into
node_modulesand commenting outenhanceCompileErrorat https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/src/utils/compile.js#L148 reveals the true error beneath.Side node:
enhanceCompileErrorshould probably be wrapped in its own try-catch block, so that if it fails (like it does here) it doesn’t obscure the actual error being thrown)Actual error:
This is most likely caused by this recent PR https://github.com/Rich-Harris/esrap/pull/13
Adding the following to https://github.com/Rich-Harris/esrap/blob/main/src/handlers.js#L560, reveals more:
logged result:
And that’s as far as I got… 😴
The workaround is stated above:
@midirhee12 Add the following to the
package.jsonfile.About
overrides:please try downgrading esrap to ~1.2.0 via override in package.json
opened a fresh issue on
sveltejs/sveltehttps://github.com/sveltejs/svelte/issues/14743Please don’t comment on closed issues, it’s easy for updates to get missed
@webJose Running
pnpm install esrap@1.2.0on a clean install does not fix the issue. Is there something I’m missing here?Also, instead of telling people to “read the thread”, it would be better to link the actual comment of the thread which noted the workaround.
This is the correct temporary fix pending an update to the
esrappackage.confirmed a clean project using
npx sv createtriggers the issue when typescript is added to the+page.svelte