-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript: IDE reports TS2307: Cannot find module error for Vue components imports #1198
Comments
Even though I don't know the reason, but this error may be caused by the new version of import Vue, { VNode } from 'vue';
declare module '*.vue' {
import Vue, { VNode } from 'vue';
export default Vue;
}
declare global {
namespace JSX {
// tslint:disable no-empty-interface
interface Element extends VNode {}
// tslint:disable no-empty-interface
interface ElementClass extends Vue {}
interface IntrinsicElements {
[elem: string]: any;
}
}
} However, just change it to the old version (like beta-6.0) declare module '*.vue' {
import Vue from 'vue';
export default Vue;
} You can resolve the modules |
This is weird because the build actually works fine, it's only the IDEs complaining. /cc @ktsn @HerringtonDarkholme any idea why importing Vue at the root level in |
ts-loader has special handling for Vue file which doesn't affect IDE. The workaround is to split The first And finally it seems that TS cannot mix up two modules, sadly. The compiler thinks one file has one single module type. So the error occurs. |
Thanks for the explanation. I knew that creating a separate file solves the problem (I found it by accident) but I didn't have the slightest idea why it helps. |
Hey @yyx990803, is a release with the fix for this published? I manually made the change from 51c8090 in my project, but the IDE still glows red, so I'd like to see if a newly generated project would work... |
I see the error in VSCode but also get this error when I do
Should the fix for the issue showing up in VSCode also fix it in |
Hello! How do you guys found that Typescript accepts glob pattern for modules name at all in declare expression? Just can`t find it in Typescript specs |
Sorry to resurrect, but this still pops up. I've actually put a // @ts-ignore on it, and it's compiling fine.. |
I also encountered the same problem, have you solved it? |
This is happening when the |
@medeman I am not using cli. Adding shim-vue.d.ts to the project & .vue extension to path of importing file not solved this problem for me =( |
And I forced to dance with .vue files only because of unit testing (@vue/test-utils can't mount vue-class-component (.ts files with string template)) =( I use SystemJS & ES6 bundling & module resolving (here my project). |
By the way is need to check that your ts code in component exported default. |
Still an issue it seems! I added the following into
|
For testing in jest I'm not need in .vue files. Solved. |
I solved this issue by adding additional declarations in shim-vue.d.ts file (all my vue files are into spa/page and spa/components directories expect for App.vue):
|
life saver |
I was having this issue only in WebStorm and not in VSCode. It was not happening with the
Note: I did not implement any of the above-mentioned declaration stubs. Doing so actually broke the vue-cli v3.4.1 |
@a-menshchikov live savior 💯 |
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:
|
@Danielg212 thank u. resolve in my When I have a
|
It is expected behavior. See: #5549 (comment) |
Is there a reason not to use this in the index.d.ts instead?
IIRC, Vue Single-File-Components export |
I restarted webstorem and this problem was solved. But I don’t know why |
This resolved .ts import .vue issue, but it's really wired to touch a declared file in the project 😕 |
I had this issue in the served webpage instead of webstorm. Fixed after stopping and restarting |
It helped me that I created another additional file with a different name: 'vue-shims.d.ts ' and also wrote in it:
|
declare module "*.vue" { |
Creating file declare module "*.vue" {
import { defineComponent } from "vue";
const component: ReturnType<typeof defineComponent>;
export default component;
} |
For me it works too. Thank you! Could you please explain why this resolves the issue ? |
Bonus: Make sure you include the
|
Every time I create a Vue-ts project I use this types helper. I hope this type config helps c: //src/shims-vue.d.ts
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
//if you use env variables c:
declare module "*/envs.ts"; |
May be a stupid answer but it happened to me this week - I was opening the root of our project structure as a workspace in vscode, not the front-end vue.js subfolder. When I opened the correct root folder this error went away. |
For me this was the real issue. |
For me installing TypeScript Vue Plugin (Volar) for vscode fixed to resolve .vue files for ts. |
for me, creating a vue3(3.3.4) project without TypeScript support has the same problem.(ts(2307)) |
Same issue when working with Nuxt 3 and Vuetify:
|
In webstorm,the following worked for me. Uncheck the vuejs and vite plugins temporarily and then re-enable them. Restart the IDE afterwards. |
(see https://github.com/vuejs/core/blob/v3.3.9/packages/runtime-core/src/apiDefineComponent.ts) |
To complement @Danielg212 answer, that solution fixes the "import" issue, but creates a new error in the script tag. To address both issues, even if I'm not using TS, I created a file at my project's root named "tsconfig.json" with the following:
This solved all issues for me |
Version
3.0.0-beta.9
Reproduction link
https://pastebin.com/N5A5uWGv
Steps to reproduce
What is expected?
No errors are reported
What is actually happening?
IDE reports TS2307: Cannot find module error for Vue components imports. Imports are higlighted in red.
I've already reported this error here: vuejs/vue-class-component#219 because I thought that it is a
vue-class-component
issue, but it's not. It's a configuration issue.Putting the following
vue-shim.d.ts
declaration file under my ./src directory solves the problem:The text was updated successfully, but these errors were encountered: