Module build failed: Error: Could not find file: '[my-project-directory]\src\App.vue'

Viewed 4023

[my-project-directory] is the full path of my project.

I started off with the webpack project via "vue init webpack my-project", as per the official Vue.js documentation.

I am attempting to switch to TypeScript by adding vuetype.

I've added a resources.d.ts file to the root of my src directory, though I'm unsure how it's being referenced:

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

I've switched the entry to ts in webpack.base.conf.js:

    var path = require('path')
    var utils = require('./utils')
    var config = require('../config')
    var vueLoaderConfig = require('./vue-loader.conf')

    function resolve (dir) {
      return path.join(__dirname, '..', dir)
    }

    module.exports = {
      entry: {
        app: './src/main.ts'
      },
      output: {
        path: config.build.assetsRoot,
        filename: '[name].js',
        publicPath: process.env.NODE_ENV === 'production'
          ? config.build.assetsPublicPath
          : config.dev.assetsPublicPath
      },
      resolve: {
        extensions: ['.js', '.ts', '.vue', '.json'],
        alias: {
          '@': resolve('src')
        }
      },
      module: {
        rules: [
          {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: vueLoaderConfig
          },
          {
            test: /\.js$/,
            loader: 'babel-loader',
            include: [resolve('src'), resolve('test')]
          },
          {
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('img/[name].[hash:7].[ext]')
            }
          },
          {
            test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('media/[name].[hash:7].[ext]')
            }
          },
          {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
            }
          }
        ]
      }
    }

This is my main.ts file. Note that if I change the import to './App' (without the .vue, it gives me "(2,17): error TS2307: Cannot find module './App'.":

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

new Vue({
  el: '#app',
  render: h => h(App)
})

In App.vue I've added the lang="ts", used Component from the vuetype libraries:

<template>
   <div>TEST</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Component } from 'vue-property-decorator'

@Component
export default class App extends Vue  {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

The App.vue.d.ts output:

import Vue from 'vue';
export default class App extends Vue {
    name: 'app';
    components: {
        Hello;
        Game;
    };
}

My package.json:

  {
    "name": "dominant-species",
    "version": "1.0.0",
    "description": "A Game",
    "author": "Frank Rizzi",
    "private": true,
    "scripts": {
      "dev": "node build/dev-server.js",
      "start": "node build/dev-server.js",
      "build": "node build/build.js"
    },
    "dependencies": {
      "@types/core-js": "^0.9.42",
      "core-js": "^2.5.0",
      "ts-loader": "^2.3.2",
      "vue": "^2.4.2"
    },
    "devDependencies": {
      "autoprefixer": "^7.1.2",
      "babel-core": "^6.22.1",
      "babel-loader": "^7.1.1",
      "babel-plugin-transform-runtime": "^6.22.0",
      "babel-preset-env": "^1.3.2",
      "babel-preset-stage-2": "^6.22.0",
      "babel-register": "^6.22.0",
      "chalk": "^2.0.1",
      "connect-history-api-fallback": "^1.3.0",
      "copy-webpack-plugin": "^4.0.1",
      "css-loader": "^0.28.0",
      "cssnano": "^3.10.0",
      "eventsource-polyfill": "^0.9.6",
      "express": "^4.14.1",
      "extract-text-webpack-plugin": "^2.0.0",
      "file-loader": "^0.11.1",
      "friendly-errors-webpack-plugin": "^1.1.3",
      "html-webpack-plugin": "^2.28.0",
      "http-proxy-middleware": "^0.17.3",
      "opn": "^5.1.0",
      "optimize-css-assets-webpack-plugin": "^2.0.0",
      "ora": "^1.2.0",
      "rimraf": "^2.6.0",
      "semver": "^5.3.0",
      "shelljs": "^0.7.6",
      "url-loader": "^0.5.8",
      "vue-class-component": "^5.0.2",
      "vue-loader": "^12.1.0",
      "vue-property-decorator": "^5.2.1",
      "vue-style-loader": "^3.0.1",
      "vue-template-compiler": "2.4.2",
      "webpack": "^2.6.1",
      "webpack-bundle-analyzer": "^2.9.0",
      "webpack-dev-middleware": "^1.10.0",
      "webpack-hot-middleware": "^2.18.0",
      "webpack-merge": "^4.1.0"
    },
    "engines": {
      "node": ">= 4.0.0",
      "npm": ">= 3.0.0"
    },
    "browserslist": [
      "> 1%",
      "last 2 versions",
      "not ie <= 8"
    ]
  }

My tsconfig.json:

  {
    "compilerOptions": {
      // ... other options omitted
    "experimentalDecorators": true,
      "allowSyntheticDefaultImports": true,
      "lib": [
        "es6", "es2015.promise", "es2015.iterable", "dom","dom.iterable","es2015"
      ],
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true, 
      "target": "es6",
      "noImplicitAny": false
    },
    "exclude": [
      "node_modules"
    ]
  }

And here is the full error:

Module build failed: Error: Could not find file: '[MyDirectory]\src\App.vue'.



error  in ./src/App.vue

Module build failed: Error: Could not find file: '[my-project-directory]\
src\App.vue'.
    at getValidSourceFile ([my-user-directory]\npm\node_modules
\typescript\lib\typescript.js:89078:23)
    at Object.getEmitOutput ([my-user-directory]\npm\node_modul
es\typescript\lib\typescript.js:89448:30)
    at getEmit ([my-project-directory]\node_modules\ts-loader\dist\index.
js:114:43)
    at successLoader ([my-project-directory]\node_modules\ts-loader\dist\
index.js:34:11)
    at Object.loader ([my-project-directory]\node_modules\ts-loader\dist\
index.js:21:12)

@ ./src/App.vue 8:2-94
@ ./src/main.ts
@ multi ./build/dev-client ./src/main.ts

Versions:

node -v
v6.11.2

npm -v
3.10.10

tsc -v
Version 2.4.2
4 Answers

did you use ts-loader?

 {
    test: /\.ts$/,
    exclude: /node_modules|vue\/src/,
    loader: 'ts-loader',
    options: {
      appendTsSuffixTo: [/\.vue$/]
    }
  }

put this in webpack.base.conf.js

I had the same problem, and I tried applying the suggestions from the above answers, but I still kept getting the same error. What did the trick for me was changing my main.js to main.ts, and also changing the entry point in build/webpack.base.conf.js to reflect the new ending.

Related