Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode showing "cannot find module" TS error for .vue import while compiling doesn't

Simply put, vscode is showing this error in a module:

Cannot find module '@/components/SidebarToggleIcon'

But no such error shows up during compilation.

This is a VueJS project and SidebarToggleIcon is a .vue file with TypeScript in the <script lang="ts"> section. This error was showing up before in VSCode and during compilation until I added the @vue/eslint-config-typescript package. Now is just shows up in VSCode.

Sidebar.vue

<script lang="ts">
// [skip other imports]
import SidebarToggleIcon from '@/components/SidebarToggleIcon';

@Component
export default class LayoutSidebar extends Vue {

    get sidebarCollapsed(): boolean {
        return preferenceModule.sidebarCollapsed;
    }
}

</script>

SidebarToggleIcon.vue

<script lang="ts">
import Vue from 'vue';
import { getModule } from 'vuex-module-decorators';
import Component from "vue-class-component";
import PreferencesStore from '@/store/PreferencesStore';

const preferenceModule: PreferencesStore = getModule(PreferencesStore);

@Component
export default class SidebarToggleIcon extends Vue {

    get sidebarCollapsed(): boolean {
        return preferenceModule.sidebarCollapsed;
    }

    toggle(){
        preferenceModule.ToggleSidebar();
    }
}
</script>

enter image description here

Why is this? How do I solve this?

Edit: This is not an issue with the @ alias, those resolve correctly (in the screenshot the line above the error uses it, and I use it else-wear in the project), this error still shows up when using relative paths. My TSConfig has the appropriate "paths": { "@/*": ["src/*"] } item. If this was the issue compiling would also throw this error, which it does not, this is only present in VSCode.

like image 672
Douglas Gaskell Avatar asked Feb 23 '19 06:02

Douglas Gaskell


People also ask

Can not find module TS?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

How do I import components into Vue 2?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

How do I import files to Vue?

There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.


3 Answers

This is because TypeScript does not resolve webpack aliases automatically.

For TS to resolve aliases, they should be added in tsconfig.json under compilerOptions.paths:

{  
  "compilerOptions": {
   "paths": {
     "@/*": [
      "./*"
     ]
    }
  }
}
like image 80
aBiscuit Avatar answered Oct 07 '22 00:10

aBiscuit


For me, for a Vite & Vue 3 project in PhpStorm it was enough to create a shims file in the src directory. Most of my components use only script setup.

src/shims-vue.d.ts

declare module '*.vue';
like image 32
hatef Avatar answered Oct 07 '22 00:10

hatef


In the Sidebar.vue file, try to add the ".vue" extension in import declaration , something like this:

import SidebarToggleIcon from '@/components/SidebarToggleIcon.vue';
like image 22
Robson Oliveira Avatar answered Oct 07 '22 00:10

Robson Oliveira