找不到模块:错误:无法解析vue,路径不正确

时间:2018-06-11 20:16:53

标签: javascript webpack vue.js

我在子文件夹中有我的webpack.config.js和node_modules。如果我尝试执行:npm run build,我会收到错误:ERROR in ../public/Vue/Components/Rating.vue Module not found: Error: Can't resolve 'vue'。我认为路径不正确,但我尝试了3个小时,但我没有找到解决方案。

文件夹结构如下:

project/
 +public/
    Vue/
      Components/
         Rating.vue
      main.js
    Dist/
 +webpack/
   webpack.config.js
   package.json
   node_modules/

webpack.config.js

var path = require('path');
var webpack = require('webpack');

var target = '../public/Vue/main.js';

var output = {
    path: path.resolve(__dirname, '../public/Dist/'),
    filename: 'default.js'
}


module.exports = {
  context: __dirname,
  entry: target,
  output: {
    path: output.path,
    filename: output.filename
  },

  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json'],
  },

module: {
    rules: [
      ... // load loader
    ]
  },
 ....
}

main.js

import Vue from 'vue';
import Rating from "./Components/Rating.vue";

new Vue({
  el: '#app',

  data() {
    return {
      content: "Hello World",
    }
  },

  components: {
    Rating,
  }
});

我希望有人可以帮忙:)

错误消息

ERROR in ../public/Vue/main.js
Module not found: Error: Can't resolve 'vue' in 'C:\xampp\htdocs\project\public\Vue'
 @ ../public/Vue/main.js 1:0-22


ERROR in ../public/Vue/Components/Rating.vue
Module not found: Error: Can't resolve 'vue-style-loader' in 'C:\xampp\htdocs\project\public\Vue\Components'
 @ ../public/Vue/Components/Rating.vue 2:2-297
 @ ../public/Vue/main.js

2 个答案:

答案 0 :(得分:0)

看起来就像你没有运行npm install

时可能得到的东西

您可以检查vue中是否存在C:\xampp\htdocs\project\public\Vue\node_modules文件夹?

答案 1 :(得分:0)

此问题在google中排名很高,因此我将为Vue 3添加设置。使用Webpack 5和Vue 3,webpack config 别名将更改为:

resolve: {
    extensions: [ '.tsx', '.ts', '.js', '.vue' ],
    alias: {
        'vue': '@vue/runtime-dom'
    }
},

希望它可以为其他人节省时间。