language-tools: Typescript cannot find module using @ root path syntax in a project created from create-vue
I’ve got a weird one. Originally posted in https://github.com/vuejs/create-vue/issues/35
Summary
When creating a project in the new recommended way using npm init vue@3, @ root path imports show an error in .vue files. @ root path imports work fine inside .ts files. The strange thing is others haven’t been able to reproduce my problem. See the issue mentioned above.
Setup
Visual Studio code - up to date Vue Language Features (Volar) - v0.31.1 TypeScript Vue Plugin (Volar) - v0.31.1 Windows 11
Steps to reproduce
npm init vue@3
Typescript support: yes
Everything else: no
pnpm install (or npm install)
Open App.vue and change an import to use @/ root path syntax. E.g. import TheWelcome from '@/components/TheWelcome.vue'. I get an error: Cannot find module '@/components/TheWelcome.vue' or its corresponding type declarations.ts(2307)
Things I tried
- Disabling all other plugins
- Uninstalling/reinstalling Volar plugins
- Enabled Take Over Mode
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 10
- Comments: 48 (9 by maintainers)
It seems that Windows will not allow use
"paths": { "@/*":["src/*"] },, try change it to"paths": { "@/*":["./src/*"] },.@johnsoncodehk I have the same problem as @alexvoedi in .vue files in projects created from create-vue.
"src/**/*.vue"is definitely inincludein tsconfig.tsconfig:
@pholly have you try config
"paths": { "@/*":["./src/*"] },in tsconfig on the project?also got this tips everything works fine before I use monorepo structure
I have the same problem. Something I noticed:
In newly created *.vue files VS Code does not find tsconfig.json.
For me it worked after I added to
srcdirectory aenv.d.tsfile with the following:@johnsoncodehk @alexvoedi @saeedtabrizi I also had the same issue today after scaffolding a brand new project using vite, it got resolved by simply adding “moduleResolution”: “node” in compilerOptions object of tsconfig.app.json
@alexvoedi make sure you have add your vue files to
includeoption of tsconfig: https://github.com/johnsoncodehk/volar-starter/blob/7a4b2f99103f5eab0bad76365be841f264623211/tsconfig.json#L14@johnsoncodehk ok, I tried it, this problem only occur at volar 0.38.8
My environment
Reproduction steps
npm init vue@latest, docnpm init vite@latest, docyou’d better run
npm installfor there 3 projectopen a VS Code window, make sure it’s workspace is empty, the window bottom color is blue (not purple)
drag [vue3-create] to VS Code window, and it will looks like
drag [vite-create] to VS Code window, and drag [old-project] to VS Code window
now there 3 project in VS Code window, and there are in same workspace.
open [old-project]/src/views/home.vue and you will look this:
Personal summary
I think this problem must be related to the workspace of vscode, and I’m sure it happened in volar 0.38.8
I found some phenomena in the process of trying to find the problem.
VSCode open a pure project (no workspace), i use above [old-project], it’s always right.
in above case, in [vue3-create] project, some component import by “@xxx” can be distinguish, but some cannot (like <TestComponentVue/>).
in [vue3-create]/src/App.vue, <HelloWorld /> tag, put mouse on it can get right tips, but cannot use Ctrl+Click to open it’s file.
in [old-project]/src/vies/home.vue, <HelloWorldVue/> tag can use Ctrl+Click to open it’s file. in this file, only <XButton> and <XComponent> is wrong, becase their path is deeper than others
in a few case, VSCode open [old-project] some .vue file, the right-bottom “tsconfig.json” cannot be read, it’s only show “no tsconfig”. it’s ok when i restart VSCode.
@xiaoxiangmoe I can’t reproduce anymore -
npm init vue@3works now. I just tried it 3 times and in all 3 projects tsconfig was recognized in .vue files. The screenshot above was from the same command a few days ago. I see that create-vue npm package was just updated a few hours ago - maybe that’s why I have no issue now.@johnsoncodehk I have no problems with projects created from create-vue now.
Thank you both.
@pholly Can you give a minimal reproducible demo?
Can you try to reproduce it in a new VSCode environment? For example:
FWIW I had this same issue in a (large) commercial Vue3 app that I am trying to migrate to TypeScript.
I started out by scaffolding out a brand new project with create-vue, choosing Yes for the TypeScript option, and then copying the generated config from the new app to the existing app.
Everything was kind of working, but I could not get rid of the red underlining under every single import path that used
@/...I tried most of the suggestions here, disabled extensions in VSCode, added extensions in VSCode, changed module resolution to node, etc, etc. But still the problem persisted.
The only thing that worked for me was to move the
baseUrlandpathsoptions from tsconfig.app.js up to the root tsconfig.json file. Then, finally, the red underlining under my imports vanished. It seems VSCode, or the official (recommended) Vue extension, loads only the default tsconfig file, not the tsconfig.app version…The vscode right-bottom “No tsconfig” maybe just caused by incorrect include config.
./src/**/*.vuedidn’t match vue files out ofsrcdir, use./**/*.vueinstead. see tsconfig#includeI found a new phenomenon right now
2 project in 1 workspace, the first project is right, second project wrong
change the project order, result changed
Interesting~
I have the same problem, i have not change any file, it was normal to open it yesterday, but now this problem happened.
This is a windows10 PC, volar auto update to v0.38.7
the problem looks like:
I try there things:
As an obsessive-compulsive disorder, I feel hopeless…
But in the end, I found the problem. It seems to be related to VSCode workspace when i pick the project to a new VSCode window, it fixed. And when i open it in the old workspace, this problem reappears.
Bad workspace, “…/…/tsconfig”
Good workspace
you can see right-bottom, “tsconfig.app.json”, It seems that volar read the root directory of the project incorrectly
I got same problem recently, without any update in my project.