模块构建失败:错误:找不到文件:'[my-project-directory] ​​\ src \ App.vue'

时间:2017-08-08 16:04:50

标签: typescript webpack vue.js

[my-project-directory]是我项目的完整路径。

根据官方的Vue.js文档,我从“vue init webpack my-project”开始使用webpack项目。

我试图通过添加vuetype来切换到TypeScript。

我已将resources.d.ts文件添加到我的src目录的根目录,但我不确定它是如何引用的:

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

我在webpack.base.conf.js中将条目切换为ts:

    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]')
            }
          }
        ]
      }
    }

这是我的main.ts文件。请注意,如果我将导入更改为'./App'(没有.vue,它会给我“(2,17):错误TS2307:找不到模块'。/ App'。”:

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

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

在App.vue中,我添加了lang =“ts”,使用了vuetype库中的Component:

<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>

App.vue.d.ts输出:

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

我的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"
    ]
  }

我的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"
    ]
  }

这是完整的错误:

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

版本:

node -v
v6.11.2

npm -v
3.10.10

tsc -v
Version 2.4.2

4 个答案:

答案 0 :(得分:2)

你使用过ts-loader吗?

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

把它放在webpack.base.conf.js

答案 1 :(得分:1)

Typescript doesn't work with the file extensions in the import statements。此外,自{v2.2起vue has had typescript support

如果您阅读了关于打字稿的链接vuejs指南,它应该对您的具体设置有所了解。它引用了a decorator style方法来声明您可能觉得有用的组件。

也许play with this cli tool可以看到webpack + typescript的样子,它可以帮助您更好地了解如何进行自己的迁移。

所以,也许尝试使用没有vuetype的Vue + Webpack + Typescript - 虽然如果你需要生成旧式组件的.d.ts文件,vuetype仍然有用,我想...我不会...我知道,我坚持使用单文件组件样式。

答案 2 :(得分:1)

我发现导致此特定错误的问题。虽然我还在处理其他问题,但这是我具体问题的答案。这是来自vuetype的输出文件是&#34; App.vue.d.ts&#34;,因此导入&#39; ./ App.vue&#39;正在关注&#34; App.vue&#34;文件不直接&#34; App.vue.d.ts&#34;。我可以通过将导入更改为&#34; ./ App.vue.d&#34;来验证这一点。它会找到正确的。因此,它选择了与部分文件名匹配的确切文件名匹配,这非常有意义。

作为旁注,如果我想继续使用vuetype来解决我的问题,而不是像Sean建议的那样使用另一个cli,那么我需要弄清楚如何在其他地方写出d文件或引用它们一些其他方式。

具有讽刺意味,因为&#39; ./ App.vue.d.ts&#39;由于其扩展(不是&#39;)而无法解决,但是&#39; ./ App.vue&#39;确实因为它的延伸而解决。

此外,似乎vuetype并不只是寻找.vue文件,它以某种方式跟随导入,因此如果我将导入设置为&#39; ./ App.vue.d& #39;,它将尝试发出名为&#34; App.vue.d.ts&#34;的文件。 for file&#34; App.vue.d.ts&#34;并抛出一个覆盖错误(除了它看起来很糟糕!)。

答案 3 :(得分:0)

我遇到了同样的问题,我尝试应用上述答案中的建议,但仍然出现相同的错误。对我来说,诀窍是将 main.js 更改为 main.ts,并更改 build/webpack.base.conf.js 中的入口点以反映新的结尾。