首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Vue-Application中导入时找不到npm包中的Sass文件

在Vue-Application中导入时找不到npm包中的Sass文件
EN

Stack Overflow用户
提问于 2017-10-26 20:15:00
回答 2查看 1.5K关注 0票数 1

当我试图导入material-components-web/material-components-web (我已经安装了npm)时,通过SASS我得到了一个错误,告诉我这个文件是找不到的或者是不可读的。

我在主App.vue文件中进行了导入:

代码语言:javascript
复制
<style lang="sass">
    @import "material-components-web/material-components-web"
</style>

目录如下:

代码语言:javascript
复制
node_modules/
  (lots of packages)
  @material/
    (lots of material components)
  material-components-web/
    material-components-web.scss
src/
  App.vue
  main.js
index.html
package.json
webpack.config.js

我第一次尝试修复这个:

显然,我首先想的是,路径是不正确的,并且尝试导入../node_modules/material-components-web/material-components,这是一种工作。然而,material-components-web也在导入一些东西(每个单独的材料组件都来自@material)。因此,即使加载了material-components-web,它还是会崩溃的。

我还在material-components-web的vue示例的源代码中看到,他们只是在导入material-components-web/material-components,而不是../node_modules/material-components-web/material-components,所以问题必须发生在其他地方。

于是我绊倒了一些有类似问题的人。他们都被告知在webpack配置中实现如下内容:

代码语言:javascript
复制
{
    loader: 'sass-loader',
    options: {
      sourceMap: true,
      includePaths: glob.sync('packages/*/node_modules').map((d) => path.join(__dirname, d)),
    },
}

我也试过了。没起作用。我克隆了vue-示例的材料-组件-网络,这也令人震惊地没有工作。(虽然它应该有效,对吗?我是说这是官方的例子)

所以我的问题是:这是个虫子吗?如果不是我做错了什么?

这里有一些更多的代码来获得一个概述:

全误差

代码语言:javascript
复制
    ./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的相关部分

代码语言:javascript
复制
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'
}

App.vue

代码语言:javascript
复制
<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>

main.js

代码语言:javascript
复制
import Vue from 'vue'
import App from './App.vue'

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

回答 2

Stack Overflow用户

发布于 2018-04-11 19:27:36

我遇到了这个问题,我的解决方案是,我从一个子文件夹导入了配置的一部分,这导致@import试图从包含我配置的子文件夹下的一个不存在的node_modules文件夹导入‘@material/按钮/mdc-按钮’。在webpack构建错误中,请看一下它试图导入的路径。你的scss装载机会在呕吐时打印出来。

票数 0
EN

Stack Overflow用户

发布于 2018-12-18 13:39:17

我也有过类似的问题。从错误中可以看出,Webpack试图从与.vue文件相同的目录中导入这些文件。我的解决方案是修改webpack配置,以包括node_modules文件夹。然而,如何做到这一点还不清楚。

对于Vue CLI,您可以从vue.config.js文件中执行如下操作:

代码语言:javascript
复制
module.exports = {
  // ... other settings ...
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.scss$/,
          use: [
            {
              loader: 'sass-loader',
              options: {
                includePaths: ['node_modules']
              }
            }
          ]
        }
      ]
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46963043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档