question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when importing components without .vue extension in TypeScript

See original GitHub issue

Version

v2.4.3

Reproduction link

https://codesandbox.io/s/zrl1qrlo3p

Steps to reproduce

Go to the CodeSandbox reproduction link and open the terminal window.

What is expected ?

Resolve .vue files without throwing errors in the terminal.

What is actually happening?

It resolves .vue files but still throws an error in the terminal.

ERROR  in ~/pages/index.vue 
Cannot find module '~/components/HelloWorld'.

import HelloWorld from "~/components/HelloWorld";

screenshot_1

<div align="right">This bug report is available on Nuxt community (#c8696)</div>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

4reactions
kevinmarreccommented, Feb 20, 2019

@cretueusebiu Alright, I indeed have been able to see the error by triggering a change.

Well you need to add .vue extension when importing Vue SFC’s.

import HelloWorld from "~/components/HelloWorld.vue";

Implicit module imports (not providing extension of the imported module) should (IMO) only be done for .js & .ts extensions.

HelloWorld.js => import HelloWorld from "~/components/HelloWorld"; HelloWorld.ts => import HelloWorld from "~/components/HelloWorld"; HelloWorld.vue => import HelloWorld from "~/components/HelloWorld.vue";

If the extension is implicit, I guess it can’t be handled properly by the ts-loader used to handle Vue SFC with TS. And there isn’t workaround (As far as I know) other than providing the .vue extension in the import statement.

1reaction
kevinmarreccommented, Feb 20, 2019

@cretueusebiu It’s cause of the TypeChecker which run in seperate process. See https://github.com/Realytics/fork-ts-checker-webpack-plugin#vue

To activate TypeScript in your .vue files, you need to ensure your script tag’s language attribute is set to ts or tsx (also make sure you include the .vue extension in all your import statements as shown below):

I’m closing this issue as it’s documented in the TypeChecker documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove .vue extension from imports when using ...
the compiler throwing an error in "Cannot find module '@/components/HelloWorld'";. The component HelloWorld exists. However, if I remove the ...
Read more >
TypeScript unable to locate module (vue file) when trying to ...
It turns out TypeScript will not recognize Vue files without adding the ".vue" extension in the path when importing. I will need to...
Read more >
Features | Vite
TypeScript #. Vite supports importing .ts files out of the box. Vite only performs transpilation on .ts files and does NOT perform ...
Read more >
Evan You on Twitter: "@murilobd You should always use the ...
You should always use the .vue extension when importing Vue components, as omitting the extension creates numerous problems for static analysis ...
Read more >
An import path cannot end with a '.tsx' extension in TS
The error "An import path cannot end with a '.tsx' extension" occurs when we include the extension when importing TypeScript files in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found