Skip to content
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

Cannot find module 'xxxx' when using path mapping #762

Closed
3 tasks done
mei-rune opened this issue Apr 14, 2018 · 119 comments · Fixed by #2525
Closed
3 tasks done

Cannot find module 'xxxx' when using path mapping #762

mei-rune opened this issue Apr 14, 2018 · 119 comments · Fixed by #2525

Comments

@mei-rune
Copy link

Info

  • Platform: Win
  • Vetur version : 0.11.7
  • VS Code version:1.22.1

Problem

default

Reproducible Case

npm install -g @vue/cli
vue create explorer
example.zip

@HerringtonDarkholme
Copy link
Member

Can you reproduce it in plain TypeScript?

@mei-rune
Copy link
Author

@HerringtonDarkholme
Copy link
Member

screen shot 2018-04-16 at 4 32 21 pm

I cannot reproduce it.

@ghost
Copy link

ghost commented Apr 9, 2019

Same problem here.
Can anybody help?

image

Without @ it works:
image

@ghost
Copy link

ghost commented Apr 10, 2019

https://github.com/vuejs/vetur/blob/master/docs/FAQ.md#vetur-cant-recognize-components-imported-using-webpacks-alias

same problem 😥
image

@octref octref reopened this Apr 10, 2019
@AtofStryker
Copy link

this also seems to be a problem on our end as well

@kinghat
Copy link

kinghat commented Apr 25, 2019

im getting this as well:

image

https://github.com/vuejs/vetur/blob/master/docs/FAQ.md#vetur-cant-recognize-components-imported-using-webpacks-alias

this didnt seem to work for me:

// webpack
module.exports = {
  resolve: {
    alias: {
      '@': 'src'
    }
  }
}```

@octref
Copy link
Member

octref commented Apr 26, 2019

@k1nghat I clarified the doc, sorry:

// If you have below Webpack config
module.exports = {
resolve: {
  alias: {
    '@': 'src'
  }
}
}
// You need such tsconfig.json
{
"compilerOptions": {
  "baseUrl": ".",
  "paths": {
    "@/*": [
      "src/*"
    ]
  }
}
}

@kinghat
Copy link

kinghat commented Apr 26, 2019

i get this when i put it into vue.config.js and try to build:

ERROR Invalid options in vue.config.js: "resolve" is not allowed
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@octref
Copy link
Member

octref commented Apr 26, 2019

@k1nghat No, my point was "if you have such webpack.config.js" you need to create a tsconfig.json in your project root. vue.config.js is an abstraction on top of webpack.config.js.

Can you clone this repo https://github.com/octref/veturpack to create a repro case?

@kinghat
Copy link

kinghat commented Apr 27, 2019

@k1nghat No, my point was "if you have such webpack.config.js" you need to create a tsconfig.json in your project root. vue.config.js is an abstraction on top of webpack.config.js.

Can you clone this repo https://github.com/octref/veturpack to create a repro case?

since the last couple updates pushed recently i havent seen the issue in my project or the cloned veturpack repo.

@octref
Copy link
Member

octref commented Apr 27, 2019

@Grewamor Are you on Windows too?

I start to doubt it's because Windows's specific path handling problem.

@ghost
Copy link

ghost commented Apr 27, 2019

Yes, windows

@vladfrangu
Copy link

I also encounter this issue, but seems like a window reload fixes it, so surely it isn't just Windows being Windows, right? 😅

@nexorianus
Copy link

I also encounter this issue, but seems like a window reload fixes it, so surely it isn't just Windows being Windows, right? 😅

A window-reload does fix the Problem!
And it's not only a Windows problem... getting the same behavior on Mac.

@octref octref changed the title Cannot find module 'xxxx' Cannot find module 'xxxx' for using path mapping May 16, 2019
@octref octref changed the title Cannot find module 'xxxx' for using path mapping Cannot find module 'xxxx' when using path mapping May 16, 2019
@forbjok
Copy link

forbjok commented May 20, 2019

I'm having this issue as well with everything except the .vue modules.
I already have the "@/*" path in tsconfig.json (it was there by default when I created the project, I believe), and no amount of restarting VSCode or even doing "git clean -fdx" and reinstalling npm packages seems to make any difference. Getting the same thing both on my laptop and my desktop machine.

@sermunar
Copy link

sermunar commented Jun 3, 2019

I am facing this issue as well in .vue files

issue in .vue files
image

no issue in .ts files
image

@iliiliili
Copy link

same
2019-06-10_13-33-33
just for disable this warnings i use direct path
2019-06-10_13-34-15

@cyberbit
Copy link

cyberbit commented Jun 11, 2019

I am also experiencing this issue on Mac.

VS Code: 1.35.0
Vetur: 0.21.0

In a typical generated Home.vue:
image

@waghcwb
Copy link

waghcwb commented Jun 12, 2019

I'm having the same issue, on Windows

image

@PatricNox
Copy link

#762 is the solution as for now, however not the real solution as this doesn't solve the issue with these types of workspaces:

Workspace 1
-- Repository 1 - Vue
---- <Here we have this issue>
-- Repository 2 - Laravel

The solution as aforementioned is to open repository #1 in its own workspace, there having the package json at the root of the workspace. But not the real solution.

The style of two repos in same workspace is very typical for decoupled solutions and API integrations between two projects.

@yoyo930021
Copy link
Member

#762 is the solution as for now, however not the real solution as this doesn't solve the issue with these types of workspaces:

Workspace 1
-- Repository 1 - Vue
---- <Here we have this issue>
-- Repository 2 - Laravel

The solution as aforementioned is to open repository #1 in its own workspace, there having the package json at the root of the workspace. But not the real solution.

The style of two repos in same workspace is very typical for decoupled solutions and API integrations between two projects.

This project does not support workspace at this time.
It's deviating from the issue.
Please follow #2377

@major697
Copy link

Any ideas ?

#762 (comment)

This solution not working, I have tsconfig.json with:

...
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
...

and still I have an error:
Cannot find module '@/components/table/TableComponent.vue' or its corresponding type declarations.Vetur(2307)

@yoyo930021
Copy link
Member

Any ideas ?

#762 (comment)

This solution not working, I have tsconfig.json with:

...
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
...

and still I have an error:
Cannot find module '@/components/table/TableComponent.vue' or its corresponding type declarations.Vetur(2307)

if you set baseUrl?
You can provide a reproducible project.

@major697
Copy link

@yoyo930021

if you set baseUrl?
You can provide a reproducible project.

I have baseUrl. My tsconfig.json file:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
  "files": [
    "./src/shims-vue.d.ts"
  ]
}

@yoyo930021
Copy link
Member

@yoyo930021

if you set baseUrl?
You can provide a reproducible project.

I have baseUrl. My tsconfig.json file:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
  "files": [
    "./src/shims-vue.d.ts"
  ]
}

Please provide a reproducible project.
https://github.com/vuejs/vetur/blob/master/.github/NO_REPRO_CASE.md
Otherwise we don’t have enough information to help you.

@yes1am
Copy link

yes1am commented Nov 4, 2020

In my cases, add file extension solve the problem

bad:

import Hello from '@/component/Hello'

good:

import Hello from '@/component/Hello.vue'

see: https://github.com/vuejs/vetur/blob/master/docs/FAQ.md#vetur-cannot-recognize-my-vue-component-import-such-as-import-comp-from-comp

@duck-dev-go
Copy link

In my cases, add file extension solve the problem

bad:

import Hello from '@/component/Hello'

good:

import Hello from '@/component/Hello.vue'

see: https://github.com/vuejs/vetur/blob/master/docs/FAQ.md#vetur-cannot-recognize-my-vue-component-import-such-as-import-comp-from-comp

I was already doing this and it does not work. It is driving me nut's. Anyone found a solution already?

@wuxiii
Copy link

wuxiii commented Dec 6, 2020

如果你是中文环境,请不要把工程文件夹放在有【中文】或者【带空格】的目录文件夹中,在中文环境下刚开始是正常的,过段时间就会出错。出错原因不明。
DO NOT PUT your project directory into none ascii drictory, or the directory name has a space charactor,if you put your directory into that environment, at beginning, it may be ok, but after some time, it will appear that error indicator.
标签:无法正常识别@,无法正常识别注入的$router,$message等变量
Label: Can't recognize @, can't recgnize $router or $message DI variable

it works for me. just make sure your workspace is the root directory of your project

@diegobelfiore
Copy link

diegobelfiore commented Dec 7, 2020

I get the "Cannot find module ___ or its corresponding type declarations" error in Vetur, but the project itself compiles and works just fine. I don't get this error in .ts files, only in .vue files (using the same reference path). So everything works, but I don't get type safety in .vue files because anything I import looks like an 'any' type.

@yoyo930021
Copy link
Member

I get the "Cannot find module ___ or its corresponding type declarations" error in Vetur, but the project itself compiles and works just fine. I don't get this error in .ts files, only in .vue files (using the same reference path). So everything works, but I don't get type safety in .vue files because anything I import looks like an 'any' type.

Please provide a reproducible project.
https://github.com/vuejs/vetur/blob/master/.github/NO_REPRO_CASE.md
Otherwise we don’t have enough information to help you.

And check path alias first char isn't slash for #2525

@pabloalvzal
Copy link

pabloalvzal commented Apr 10, 2021

I experienced the same problem as soon as I needed to move my root directory into a ./src directory (eg. nuxt deployment in firebase). Namely, all my @/components/MyComponent.vue could not be found and I could no longer navigate to them.

Workaround (not a solution) for VS Code
If I open VS code directly from the ./src directory as your root directory everything worked for me again. This was acceptable for me because, during development, I only need to work on this directory.

So instead of this:
cd /Users/myuser/projectName && code .

I did this:
cd /Users/myuser/projectName/src && code .

@yoyo930021
Copy link
Member

I experienced the same problem as soon as I needed to move my root directory into a ./src directory (eg. nuxt deployment in firebase). Namely, all my @/components/MyComponent.vue could not be found and I could no longer navigate to them.

Workaround (not a solution) for VS Code
If I open VS code directly from the ./src directory as your root directory everything worked for me again. This was acceptable for me because, during development, I only need to work on this directory.

So instead of this:
cd /Users/myuser/projectName && code .

Do this:
cd /Users/myuser/projectName/src && code .

Where are your tsconfig.json/jsconfig.json?
You can keep in the opened project root or use vetur.config.js

@eddow
Copy link

eddow commented Apr 10, 2021

Facing the same problem (ie. having 2 tsconfig.json: one for the client, one for the server), I found this vetur configuration, but I can't really make it work.

Is it a dead-end or am I just clumsy at configuration?

EDIT: As a workaround, what I ended up doing was to configure my root tsconfig for my client application, and letting in the server app a tsconfig with a "extends": "../../tsconfig.json",, re-setting the server-side specific configuration

@ghtali
Copy link

ghtali commented Apr 21, 2021

Info

  • Platform: Win
  • Vetur version : 0.11.7
  • VS Code version:1.22.1

Problem

default

Reproducible Case

npm install -g @vue/cli
vue create explorer
example.zip

=============================
This is just an alternative way to bypass the error and mostly for testing purposes!

Use absolute path like :

Code_s7DSxEGc8e

Instead of:

Code_vgffaiyBb0

===============

Extra tip:

Please keep in mind that you don't need to use .ts to import them like .vue files, like :

Code_CeSzIEzftY

Update: Since Vetur causing this problem, this is just an alternative and temporary way for mapping problem if you want to test and run the project.

@sinner
Copy link

sinner commented May 7, 2021

If you set the language to js in your script tag, the import errors disappear. Don't know why.

<script lang="js">
...
</script>

@HoPGoldy
Copy link

If you also have this issue in the .ts file, check your ts.config.json:

bad

{
  "compilerOptions": {
    ...
    "paths": {
      "@/":["src/"]
    }
  }
}

good

{
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
      "@/*":["src/*"]
    }
  }
}

@avinoamsn
Copy link

Please use absolute path like this:

Code_s7DSxEGc8e

@ghtali while this is a valid means of circumventing the issue, the question is about resolving the pathmapping error.

@ghtali
Copy link

ghtali commented Jun 21, 2021

Please use absolute path like this:
Code_s7DSxEGc8e

@ghtali while this is a valid means of circumventing the issue, the question is about resolving the pathmapping error.

@avinoamsn That's right but since Vetur causing this error, this could be a temporary fix to run your code. I didn't say that using absolute path will fix the problem but it's an alternative to avoid facing mapping problem.

@lucassimines
Copy link

lucassimines commented Jun 23, 2021

I was facing this issue, because my Vue Client is inside admin folder and has a tsconfig.json inside this folder. So the path inside the tsconfig.json file is not recognized by Vetur if it's not in the root folder. All I had to do was to create a new tsconfig.json file inside the root folder and paste the following:

{
  "compilerOptions": {
    "paths": {
      "@/*": ["admin/src/*"]
    }
  }
}

@matthew-dean
Copy link

matthew-dean commented Jun 30, 2021

I encountered this issue and this comment helped me to diagnose and solve it. --> edit: not solved

Essentially, I had a monorepo set up that didn't have a root-level tsconfig.json, which Vetur apparently requires. After I set up a tsconfig.json in the root, and then set up an extends in the tsconfig.json in my package folder, and rebooted VSCode, then Vetur was able to resolve paths.

So, I think there's a Vetur bug around resolving sub-projects (sub-folders with tsconfig.json files)

UPDATE: This apparently only fixed it for .vue files, for which I had a shim to help resolve. However, this is still broken:

<script lang="ts">
// Vetur can't resolve, even though TS / Webpack can resolve just fine
import { createClient } from '@/plugins/contentful' 

@eyedean
Copy link

eyedean commented Aug 6, 2021

In case your project's tsconfig.json is not in the root folder of VS Code

Vetur might not pick up the tsconfig.json automatically if it's in the subdirectories. You have to manually guide Vetur -- https://vuejs.github.io/vetur/guide/setup.html#advanced.

TL;DR: add the following to vetur.config.js in the root of the repo (that you open in VS Code).

module.exports = {
    projects: [ "./path/to/project/which/has/tsconfig.json" ]
};

@dudintv
Copy link

dudintv commented Aug 17, 2021

In case your project's tsconfig.json is not in the root folder of VS Code

Vetur might not pick up the tsconfig.json automatically if it's in the subdirectories. You have to manually guide Vetur -- https://vuejs.github.io/vetur/guide/setup.html#advanced.

TL;DR: add the following to vetur.config.js in the root of the repo (that you open in VS Code).

module.exports = {
    projects: [ "./path/to/project/which/has/tsconfig.json" ]
};

Unfortunately, this way I got some issues with VSCode & Prettier, like disabling code folding and Prettier for vue files.
I came up with this, working for me:

module.exports = {
  projects: ["./path/to/project"],
};

@resuta566
Copy link

image
why it doesn't work on the template?

I'm using nuxt.js/vue.js 2.6.14

@yoyo930021
Copy link
Member

image why it doesn't work on the template?

I'm using nuxt.js/vue.js 2.6.14

Typescript don't support svg file.
You only need to add a svg.d.ts in project source root.

declare module '*.svg';

@yoyo930021
Copy link
Member

Everyone encounters different problems:

  1. typescript's original design
  2. vetur bugs

I will lock this issue.
If you have any problem, you can refer top comments.
After confirming that there is a problem, open a new issue.

@vuejs vuejs locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.