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>
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.
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.
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.
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.
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": {
"@/*": [
"./*"
]
}
}
}
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';
In the Sidebar.vue file, try to add the ".vue" extension in import declaration , something like this:
import SidebarToggleIcon from '@/components/SidebarToggleIcon.vue';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With