VSCode Shows 'Cannot Find Module' TypeScript Error for .vue Import in VueJS – Compilation Works, Here's Why & How to Fix
If you’ve worked with VueJS and TypeScript, you’ve likely encountered a frustrating scenario: your project compiles and runs perfectly, but VSCode persistently highlights .vue imports with a red squiggly line, yelling “Cannot find module './Component.vue' or its corresponding type declarations”.
This error is confusing because the app works—so why is VSCode complaining? The short answer: VSCode’s TypeScript language server and your build tool (e.g., Vite, Webpack) use different logic to resolve modules. Your build tool processes .vue files via plugins (like @vitejs/plugin-vue), but VSCode’s TypeScript server relies on type declarations to recognize module types.
In this blog, we’ll demystify why this error occurs and walk through step-by-step solutions to fix it.
Table of Contents#
- Understanding the Problem: Why Compilation Works, but VSCode Complains
- Root Causes of the "Cannot Find Module" Error
- Step-by-Step Solutions
- Advanced Troubleshooting
- Conclusion
- References
1. Understanding the Problem: Why Compilation Works, but VSCode Complains#
Your Vue+TypeScript project compiles because tools like Vite, Webpack, or Rollup use loaders/plugins (e.g., @vitejs/plugin-vue, vue-loader) to process .vue single-file components (SFCs). These tools parse the .vue file, extract the script, template, and style, and bundle them into executable JavaScript.
VSCode, however, relies on the TypeScript language server to check for type errors. TypeScript natively doesn’t understand .vue files—it only recognizes standard module types (.js, .ts, .json, etc.). Without explicit type declarations, TypeScript can’t confirm that .vue files are valid modules, hence the "Cannot find module" error.
2. Root Causes of the "Cannot Find Module" Error#
The error boils down to TypeScript’s inability to resolve type information for .vue files. Here are the most common culprits:
1. Missing Type Declarations for .vue Files#
TypeScript requires type shims (.d.ts files) to recognize non-standard module types like .vue. Without a shim, TypeScript has no way to know that .vue files export Vue components.
2. Incorrect tsconfig.json Settings#
Misconfigured tsconfig.json options (e.g., moduleResolution, include, compilerOptions) can prevent TypeScript from locating your source files or shim declarations.
3. VSCode Using the Wrong TypeScript Version#
VSCode may default to a global TypeScript version instead of the one installed in your project. Mismatched versions can cause inconsistencies in module resolution.
4. Outdated Tooling (Vetur vs. Volar)#
For Vue 3+, Volar replaced Vetur as the official IDE tooling. Using Vetur with Vue 3 or conflicting extensions can disrupt TypeScript’s ability to resolve .vue modules.
3. Step-by-Step Solutions#
Let’s fix the error with these actionable steps, starting with the most common fixes.
3.1 Add a Type Shim for .vue Files#
The first and most critical fix is to add a type shim (declaration file) that tells TypeScript how to interpret .vue files.
How to Do It:#
- Create a file named
vue-shim.d.tsin yoursrc/directory (or project root, ifsrc/isn’t used). - Add the following content:
// src/vue-shim.d.ts
declare module '*.vue' {
import type { DefineComponent } from 'vue';
// For Vue 3: DefineComponent is the type for Vue components
const component: DefineComponent<{}, {}, any>;
export default component;
}What This Does:#
declare module '*.vue'tells TypeScript to treat all files ending in.vueas modules.DefineComponent(from Vue’s types) ensures TypeScript recognizes the module as a Vue component with props, emits, and state.
Note for Vue 2: Use Vue instead of DefineComponent:
// Vue 2 shim
declare module '*.vue' {
import Vue from 'vue';
export default Vue;
}3.2 Configure tsconfig.json Correctly#
Your tsconfig.json dictates how TypeScript compiles and resolves modules. Incorrect settings here are a common source of the error.
Key tsconfig.json Fixes:#
1. moduleResolution#
Set moduleResolution to Bundler (recommended for Vite/Rollup) or NodeNext (for modern Node.js projects). This ensures TypeScript uses module resolution logic compatible with your build tool.
// tsconfig.json
{
"compilerOptions": {
"moduleResolution": "Bundler", // or "NodeNext"
// ... other options
}
}2. include#
Ensure include covers your source files and shim. This tells TypeScript which files to type-check:
{
"include": ["src/**/*", "vue-shim.d.ts"], // Covers src/ and the shim
"exclude": ["node_modules"]
}3. allowSyntheticDefaultImports and esModuleInterop#
Enable these to support default imports for modules that don’t explicitly export a default (common in Vue SFCs):
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
// ...
}
}Full Example tsconfig.json (Vite + Vue 3):#
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Bundler", // Critical for Vite
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "vue-shim.d.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}3.3 Ensure VSCode Uses the Workspace TypeScript Version#
VSCode may use a global TypeScript version instead of the one installed in your project’s node_modules. This can cause mismatched behavior.
How to Fix:#
- Open your project in VSCode.
- Open a
.tsor.vuefile. - Look at the bottom-right corner of VSCode—you’ll see the TypeScript version (e.g., "TypeScript 5.2.2").
- Click the version number, then select "Use Workspace Version" (this uses the TypeScript installed in
node_modules/typescript).

3.4 Verify Volar Setup (Vue 3+)#
Volar is the official IDE tooling for Vue 3, replacing Vetur. It includes a TypeScript plugin that enhances .vue type support.
Steps to Ensure Volar is Working:#
- Disable Vetur: If you have Vetur installed, disable it (it conflicts with Volar for Vue 3).
- Install Volar: Install the Vue Language Features (Volar) extension.
- Enable Take Over Mode (Optional but Recommended):
Volar’s "Take Over Mode" replaces VSCode’s built-in TypeScript server with a Vue-optimized version. To enable:- Open the command palette (Ctrl+Shift+P / Cmd+Shift+P).
- Run "TypeScript: Select TypeScript Version".
- Choose "Use Workspace Version" (ensure your project has
vue-tscinstalled:npm install vue-tsc --save-dev).
4. Advanced Troubleshooting#
If the above steps don’t resolve the error, try these advanced fixes:
4.1 Check for Multiple/Conflicting Shim Files#
If you have multiple .d.ts files declaring *.vue modules (e.g., in src/ and project root), TypeScript may prioritize the wrong one. Delete duplicate shims and keep only vue-shim.d.ts in src/.
4.2 Validate Import Paths#
TypeScript is case-sensitive, and relative paths must be precise. For example:
- ❌
import MyComponent from './mycomponent.vue'(incorrect case) - ✅
import MyComponent from './MyComponent.vue'(correct case) - ❌
import MyComponent from '../components/MyComponent'(missing.vueextension) - ✅
import MyComponent from '../components/MyComponent.vue'(explicit extension)
4.3 Restart the TypeScript Server#
VSCode’s TypeScript server may cache old type information. Restart it:
- Open the command palette (Ctrl+Shift+P / Cmd+Shift+P).
- Run "TypeScript: Restart TS server".
4.4 Clear VSCode Cache#
Corrupted cache can cause persistent errors. Close VSCode and delete the cache folder:
- Windows:
%APPDATA%\Code\Cache - macOS:
~/Library/Caches/Code - Linux:
~/.config/Code/Cache
4.5 Check for Monorepo Issues#
In monorepos, ensure:
- The shim file is in the package’s
src/directory. tsconfig.jsonin the package hasincludeset to its own source files.- Dependencies between packages are correctly linked (e.g., via
pnpm workspacesornpm workspaces).
5. Conclusion#
The "Cannot find module" error for .vue imports in VSCode is a TypeScript type resolution issue, not a build problem. The core fixes involve:
- Adding a
vue-shim.d.tsfile to declare.vuemodules. - Configuring
tsconfig.jsonwith propermoduleResolution,include, and synthetic import settings. - Ensuring VSCode uses the workspace TypeScript version and Volar (for Vue 3).
By addressing these, you’ll eliminate the red squiggles and keep your Vue+TypeScript project type-safe and IDE-friendly.