TypeScript 'cannot find module...' in Vue project
Asked Answered
T

6

15

I'm using TypeScript in Vue project.

But when I try to import vue file, I can't get .vue file since the 'cannot find module...' error.

enter image description here

What is weird is that intellisense only shows the directory right upper the .vue file.

Intellisense shows the directory well Intellisense shows the directory well

But it doesn't show the .vue file inside it But it doesn't show the .vue file inside it

I also have vue-shim.d.ts file in root directory.

Turbo answered 15/3, 2022 at 4:59 Comment(2)
Have you found a solution for it? I have the same issueChandelle
@RobinSchambach No I didn't... I'm using React now so I'm not facing that issue but didn't solve that.Turbo
H
12

You should install TypeScript Vue Plugin and Vue Language Features.

Heti answered 15/10, 2022 at 19:48 Comment(2)
Don't forget to disable the built-in TypeScript support in VS Code. vuejs.org/guide/typescript/overview.html#volar-takeover-modeNigh
The TS Vue Plugin is now deprecated; you should use "Vue Official" vscode plugin instead: marketplace.visualstudio.com/items?itemName=Vue.volarOccultation
T
11

Try adding the following into index.d.ts in the project root, works for now.

declare module '*.vue' {
    import Vue from 'vue'
    export default Vue
}

or try to configure the path in tsconfig with

"paths": { "@/*":["./src/*"] }

or edit includes in tsconfig with

"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] 
Tithing answered 15/3, 2022 at 5:22 Comment(1)
I think this is but a vue 2 fix, see @rjurado01 answer for vue 3.Kiely
C
5

If you are using Vue 3 you should add this into a type file like vue.d.ts:

declare module '*.vue' {
  import type {DefineComponent} from 'vue'
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const component: DefineComponent<object, object, any>
  export default component
}

You can see it working here: source

Circus answered 19/5, 2023 at 6:5 Comment(1)
vue3 also seems to be set up for the following to work as well: declare module '*.vue' { export default never }Kiely
W
4

In my case the issue is with VS Code. After doing

Shift + CMD + P  -> Restart typescript server (Mac)

The lint issue is no more.

Waldheim answered 4/10, 2023 at 6:48 Comment(1)
Is there any way to not having to do this again and again each time it brokes?Rochelrochell
A
0

For me, following resolved the issue Windows 10:

  1. Focus on any *.ts (preferably for which you are getting error)

    CTRL + SHIFT + P -> Restart TS Server
    
  2. Focus on any *.vue (preferably in which you are getting error)

    CTRL + SHIFT + P -> Restart Volar server
    
Antimere answered 10/10, 2023 at 18:28 Comment(0)
C
0

I was working on IntelliJ 2023.2.2 and I had the same issue. None of the other solution worked, until I selected "Repair IDE"

enter image description here

Cruck answered 17/10, 2023 at 10:3 Comment(1)
I was trying all the work arounds on vs-code. (Having installed the vue extensions and all.) Closing the editor and opening the project back up fixed the issue.Orchestrate

© 2022 - 2024 — McMap. All rights reserved.