TS2307: Cannot find module error but the application compiles and runs properly

See original GitHub issue

I’m trying to set up a TypeScript project with the vue-class-component decorator support.

Everything seems to be fine, the application compiles properly and works fine as well but there’s one thing that bothers me.

Code editors (both: WebStorm and Visual Studio Code) complain about the second line of the index.js file. The message is:

TS2307: Cannot find module './components/App.vue'

Here’s the code:

index.js

import Vue from 'vue';
import App from './components/App.vue';

new Vue({
  el: '#js--app',
  render (createElement) {
    return createElement(App);
  },
});

App.vue

<template>
  <button @click="onClick">Click!</button>
</template>

<script lang="ts">
  import Vue from 'vue';
  import Component from 'vue-class-component';

  @Component
  export default class App extends Vue {
    message: string = 'Hello!';

    onClick(): void {
      window.alert(this.message)
    }
  }
</script>

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES5",
    "sourceMap": true,
    "inlineSources": true,
    "importHelpers": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}

And here’s a screenshot as a confirmation :slight_smile:

406e68d7ab6eb9d0cdabb792a4e0b7adf1239749


So why am I having this error and what does it really mean?

…Actually, I know what this error means because I read through the Typescript documentation which says that this error shows up when TypeScript compiler can’t resolve a module. But it doesn’t seem to make much sense to me. If it couldn’t resolve the App.vue module then my application wouldn’t compile. Am I right?

I tried to set allowSyntheticDefaultImports to true in tsconfig.json, but it doesn’t change anything.

So I’m a bit lost now. My app compiles and runs, but from what I can see it shouldn’t 🙃


PS I don’t know is Github a proper place for such questions… but on the other hand I’m not sure is it a configuration issue or is it a bug in vue-class-component. I’ve already created a topic on the Vue.js forum, but nobody responded to it

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:21
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

43reactions
wujekbogdancommented, Feb 2, 2018

@HerringtonDarkholme Thanks a lot!

Putting the following vue-shim.d.ts declaration file under my ./src directory solves the problem:

declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}

Your vue-ts-plugin plugin solves the issue as well.

BTW, could you explain what’s the benefit of using your plugin over a simple module declaration? Is there any reason why I should use it instead of the solution that I’ve already applied?

I’m trying to develop a vue.js template based on FuseBox that anyone can use, so I prefer to keep the dependencies list as short as possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tsc throws `TS2307: Cannot find module` for a local file
ts(5,20): error TS2307: Cannot find module 'components/button'. It complains about all imports of local files, like the following: import Counter from ' ...
Read more >
Common TypeScript module problems and how to solve them
Solution 1: Locate the correct directory ; "compilerOptions": ; "baseUrl": ; ".", ; "paths": ; "express": ...
Read more >
ts2307: cannot find module 'fs' or its corresponding type ...
To solve the "Cannot find module `fs` or its corresponding type declarations" error, install the types for node by running the command `npm...
Read more >
Fix error: cannot find module 'x' in Node.js or 'express'
A quick fix to the error-cannot find module is to try and install all the presented module in your terminal correctly. Navigate to...
Read more >
cannot find module [Node npm Error Solved] - freeCodeCamp
How to Fix the "cannot find module" Error · delete the node modules folder by running rm -rf node_modules · delete package.lock.json file...
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