【问题标题】:Sass file from npm package not found when importing in Vue-Application在 Vue-Application 中导入时找不到来自 npm 包的 Sass 文件
【发布时间】:2017-10-26 20:15:00
【问题描述】:

当我尝试通过 SASS 导入我 npm 安装的 material-components-web/material-components-web 时,我收到一个错误,告诉我该文件未找到或无法读取。

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

<style lang="sass">
    @import "material-components-web/material-components-web"
</style>

目录如下:

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 配置中实现类似的东西:

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

我也试过了。没用。 我克隆了 material-components-web 的vue-example,这令人震惊地也没有用。 (虽然它应该可以正常工作?我的意思是这是官方示例)

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


这里有更多的代码来获得概览:

完全错误

    ./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'
}

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>

ma​​in.js

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

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

【问题讨论】:

    标签: webpack sass vue.js


    【解决方案1】:

    我遇到了这个确切的问题,我的解决方案是意识到我已经从一个子文件夹中导入了一部分配置,这导致@import 尝试从一个不存在的文件中导入“@material/button/mdc-button”包含我的配置的子文件夹下的 node_modules 文件夹。在 webpack 构建错误中,查看它尝试从哪些路径导入。您的 scss 加载程序会在呕吐时将它们打印出来。

    【讨论】:

    • 将路径修改为相对路径,例如@import "../../node_modules/@material/button/mdc-button"; 似乎无法解决问题。然后从该导入调用的导入失败。现在我得到File to import not found or unreadable: @material/ripple/common
    【解决方案2】:

    我遇到了类似的问题。从错误中可以清楚地看出,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']
                  }
                }
              ]
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 2016-07-09
      • 1970-01-01
      • 2014-11-09
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2020-12-10
      相关资源
      最近更新 更多