0

I am trying to setup a Vue3 project with Typescript.
I get the error: "File '/home/.../src/App.vue.ts' is not a module".

When using Javascript in main.js, it works fine, but when renaming it to main.ts, I am unable to import ".vue" files.
Main.ts:

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

createApp(App).use(router).mount('#app');

Now, when running yarn serve, the output shows:

ERROR in src/main.ts:2:17
TS2306: File '/home/.../src/App.vue.ts' is not a module.
    1 | import { createApp } from 'vue';
  > 2 | import App from './App.vue';
      |                 ^^^^^^^^^^^
    3 | import router from './router';

What is the way to make Typescript understand .vue-files?

2 Answers 2

6

It turned out that I should add "export default {}" to my App.vue file.

The original (faulty) App.vue:

<template>
  <router-view></router-view>
</template>

<script lang="ts">
console.log('Loading App.vue');
</script>

I fixed the error by adding "export default {};".
The new App.vue:

<template>
  <router-view></router-view>
</template>

<script lang="ts">
console.log('Loading App.vue');

export default {}; // <-- added this line to fix the error
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

<script> tag is optional in a .vue file. But, when it's missing, the framework provides a default export. So you can't just add one for logging. If you add it, it has to export the component logic, whether it's Options API, Composition API, or the <script setup> macro (which is an alternative, removing some boilerplate)... To better understand what Vue does behind the scenes, play in the playground and peek at the JS tab.
6

App.vue must be:

<script setup lang="ts">
<script>

or

<script lang="ts">
export default {};
</script>

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
thanks a lot. it was a silly mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.