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

image

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (8 by maintainers)

Commits related to this issue

Most upvoted comments

If I disablecheckJs in jsconfig.json the error goes away

Capture d’écran 2023-05-24 à 12 02 23

Same here, still not fixed.

Create a type file and declare module (just like shims-vue.d.ts in vue)

// inside ./src/svelte.d.ts
declare module '*.svelte'

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:

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),
	"enable-ts-plugin": true, **<-- THIS ONE**
	kit: {
		adapter: adapter()
	}
};

The installation was fresh, so this is still a bug and not fixed.

Well after adding "svelte.enable-ts-plugin": true in settings.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 .sveltes in index.ts. After reloading VSCode windows, only the active package (last focused editor editing the file within) is fine. Other packages’ index.ts are showing the Cannot 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:

It should also disappear after restarting your editor.

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 run npm 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 after npm install.