找不到模块:错误:无法解析VUE,路径不正确
我的webpack.config.js和node_module在一个子文件夹中。如果我尝试执行: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:xampphtdocsprojectpublicVue'
@ ../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:xampphtdocsprojectpublicVueComponents'
@ ../public/Vue/Components/Rating.vue 2:2-297
@ ../public/Vue/main.js
解决方案
此问题在谷歌中排名靠前,因此我要为Vue 3添加设置。对于webpack 5和Vue 3,webpack配置别名更改为:
resolve: {
extensions: [ '.tsx', '.ts', '.js', '.vue' ],
alias: {
'vue': '@vue/runtime-dom'
}
},
或者,如果是转写错误,这可能是解决方案:
alias: {
'Vue': 'vue/dist/vue.esm-bundler.js',
}
希望为他人节省时间。
相关文章