language-tools: Vscode - Cannot find module './App.svelte' or its corresponding type declarations.
Describe the bug
When the extension installed after scaffolding a project if I open main.js, vs code complains about the error even though everything is perfectly working and valid.
Reproduction
npm init vite my-app -- --template svelte
cd my-app
npm install
npm run dev
And open VS Code.
Expected behaviour
Should not complain
System Info
Windows 10 VS Code: 1.69.0 Svelte for VS Code v105.18.1
Which package is the issue about?
Svelte for VS Code extension
Additional Information, eg. Screenshots
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (8 by maintainers)
Commits related to this issue
- (fix) don't remove the .svelte definitions when ts-plugin is disabled (#1557) #1556. Also added the encoding parameter just to be safe — committed to sveltejs/language-tools by jasonlyu123 2 years ago
If I disable
checkJs
injsconfig.json
the error goes awaySame here, still not fixed.
Create a type file and declare module (just like shims-vue.d.ts in vue)
Experienced the same with a fresh install with Svelte (Typescript) via Yarn 2. Adding this in
svelte.config.js
fixed the issue temporarily, but doing changes in files outlines errors again:The installation was fresh, so this is still a bug and not fixed.
Well after adding
"svelte.enable-ts-plugin": true
insettings.json
the error disappeared. Could you explain what was the problem? Thank you so much for giving me the right solution.Ah right. that’s the problem. We indeed didn’t check the config in the “remove the ambient type definitions” logic. Ironically what I said you can try is the actual reproduction of the problem. Sorry I didn’t check it more.
It seems not working properly in a monorepo when multiple packages export their own
.svelte
s inindex.ts
. After reloading VSCode windows, only the active package (last focused editor editing the file within) is fine. Other packages’index.ts
are showing theCannot find module './xxx.svelte' or its corresponding type declarations.
error.Manually adding
xxx.svelte.d.ts
fixes the issue but not a very pleasant workflow.My hunch was that maybe the “remove the ambient type definitions” logic which we added recently also runs when the plugin is disabled, which shouldn’t happen
What happens if you turn on the plugin (it applies to JS files, too)?
Does the error go away if you disable the Svelte typescript plugin and restart vs Code?
Thanks @jasonlyu123 for this:
I ran into this same issue and it went away after restarting VS Code.
The error is coming from typescript, which also powers the javascript language feature. It’s normal to have this error for file extensions not supported by typescript. But it can be suppressed if you have a type definition for the file extensions. And the
vite-env.d.ts
is for this purpose. It just won’t work until you have installed the dependencies.Not sure what happens in your system. I can’t reproduce this when I didn’t enable the ts plugin. The error only shows up before
npm install
. Which is expected. If I open the editor first and then runnpm install
. The error would disappear after installing finished. Usually, even if the error doesn’t disappear after installing. It should also disappear after restarting your editor.The typescript plugin is another way to solve the problem by injecting logic into the tssever, which powers the js/ts language features. To allow tssever to know how to process a svelte file. This feature also doesn’t work before
npm install
. But this is because we won’t start the plugin if we detected it’s not a svelte project, for performance reasons.TLDR. Next time. You can try opening the editor after you run
npm install
. Or restart your editor afternpm install
.