Webpack 在Vue应用程序中导入时未找到npm包中的Sass文件
当我试图通过SASS导入npm安装的Webpack 在Vue应用程序中导入时未找到npm包中的Sass文件,webpack,sass,vue.js,Webpack,Sass,Vue.js,当我试图通过SASS导入npm安装的材料组件web/material components web时,我收到一个错误,告诉我该文件找不到或无法读取 我在主App.vue文件中进行了导入: <style lang="sass"> @import "material-components-web/material-components-web" </style> 我第一次尝试修复此问题: 显然,我首先想到的是,路径不正确,并尝试导入。/node\u模块/材质组件we
材料组件web/material components web
时,我收到一个错误,告诉我该文件找不到或无法读取
我在主App.vue
文件中进行了导入:
<style lang="sass">
@import "material-components-web/material-components-web"
</style>
我第一次尝试修复此问题:
显然,我首先想到的是,路径不正确,并尝试导入。/node\u模块/材质组件web/材质组件
,哪种方式有效。然而,材料组件网站
也在导入材料(来自@material
的每个材料组件)。因此,即使加载了材料组件web
,它也会崩溃
我还在材料组件web
的vue示例的源代码中看到,他们只是导入材料组件web/材料组件,而不是。/node\u模块/材料组件web/材料组件
,因此问题必须在其他地方
于是我偶然发现了一些有类似问题的人。他们都被告知在他们的网页配置中实现类似的功能:
{
loader: 'sass-loader',
options: {
sourceMap: true,
includePaths: glob.sync('packages/*/node_modules').map((d) => path.join(__dirname, d)),
},
}
我也试过了。没用。
我克隆了web的材料组件,这也令人震惊地不起作用。(虽然它应该能正常工作?我的意思是这是官方的例子)
所以我的问题是:这是一个bug吗?如果不是,我做错了什么
这里还有一些代码可供概述:
完全错误
./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-0219ec88","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js?indentedSyntax!./node_modules/vue-loader/lib/selector.js?type=styles&index=0&bustCache!./src/App.vue
Module build failed:
undefined
^
File to import not found or unreadable: material-components-web/material-components-web.
Parent style sheet: stdin
in somedirectory\src\App.vue (line 30, column 1)
@ ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-0219ec88","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js?indentedSyntax!./node_modules/vue-loader/lib/selector.js?type=styles&index=0&bustCache!./src/App.vue 4:14-342 13:3-17:5 14:22-350
@ ./src/App.vue
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
webpack.config.js的相关部分
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
App.vue
<template>
<div id="app">
<!-- Html Stuff -->
</div>
</template>
<style lang="sass">
@import "material-components-web/material-components-web"
</style>
<script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
我遇到了这个问题,我的解决方案是意识到我从一个子文件夹导入了一部分配置,这导致@import尝试从包含我的配置的子文件夹下不存在的node_modules文件夹导入“@material/button/mdc button”。在webpack生成错误中,查看它试图从哪些路径导入。你的scss加载器会在呕吐时打印出来 我遇到了这个问题,我的解决方案是意识到我已从子文件夹导入了部分配置,这导致@import尝试从包含我的配置的子文件夹下不存在的node_modules文件夹导入“@material/button/mdc button”。在webpack生成错误中,查看它试图从哪些路径导入。你的scss加载器会在呕吐时打印出来 我也有类似的问题。从错误中可以清楚地看出,Webpack试图从与.vue文件相同的目录导入文件。我的解决方案是修改webpack配置,包括node_modules文件夹。然而,如何做到这一点目前还不清楚
使用Vue CLI,可以从Vue.config.js文件执行以下操作:
module.exports = {
// ... other settings ...
configureWebpack: {
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'sass-loader',
options: {
includePaths: ['node_modules']
}
}
]
}
]
}
}
}
我也有类似的问题。从错误中可以清楚地看出,Webpack试图从与.vue文件相同的目录导入文件。我的解决方案是修改webpack配置,包括node_modules文件夹。然而,如何做到这一点目前还不清楚
使用Vue CLI,可以从Vue.config.js文件执行以下操作:
module.exports = {
// ... other settings ...
configureWebpack: {
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'sass-loader',
options: {
includePaths: ['node_modules']
}
}
]
}
]
}
}
}
将路径修改为相对路径,例如,@import./../node_modules/@material/button/mdc button”代码>似乎无法解决此问题。然后从该导入调用的导入失败。现在我得到导入的文件找不到或不可读:@material/ripple/common
将路径修改为相对路径,例如@import./../node\u modules/@material/button/mdc button”代码>似乎无法解决此问题。然后从该导入调用的导入失败。现在我得到要导入的文件找不到或不可读:@material/ripple/common