question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Impossible to import scss (vue cli 3)

See original GitHub issue

I want to use material-components-web via npm in my Vue CLI 3 project.

Here is some configuration:

App.vue

<style src="./main.scss" lang="scss"></style>

main.scss

@import "~material-components-web/material-components-web";

main.js

import Vue from 'vue'
import * as mdc from 'material-components-web';
import App from './App.vue'
import router from './router'
import store from './store'
import './registerServiceWorker'

Vue.config.productionTip = false

Vue.use(mdc)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

Error I get:

ERROR  Failed to compile with 1 errors
 error  in ./src/main.scss?vue&type=style&index=0&lang=scss&
Module build failed (from ./node_modules/sass-loader/lib/loader.js):
undefined
^
      File to import not found or unreadable: @material/button/mdc-button.
      in C:\Users\...\my-project\node_modules\material-components-web\material-components-web.scss (line 23, column 1)

I looked on the internet, including all possible GitHub related issues, stackoverflow for 5 hours now with all trial possible, the solution that got repeated is to add node modules in the includePaths.

I tried that (but in many different ways): vue.config.js

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        includePaths: ["node_modules"]
      }
    }
  }
}

I tried for example:

const path = require('path')
...
loaderOptions: {
      sass: {
        includePaths: [path.resolve(__dirname, "node_modules")],
      },
    }

or:

includePaths: glob.sync('node_modules').map((d) => path.join(root, d))

or:

includePaths: ['node_modules', 'node_modules/@material/*'] .map((d) => path.join(__dirname, d))

or:

loaderOptions: {
            sass: {
                data: [
                    '../node-modules/material-components-web/material-components-web.scss'
                ].map(fileName => fs.readFileSync(fileName, 'utf-8')).join('\n')
            }
        }

But still the same error. He finds the main scss in node_modules/material-components-web/material-components-web.scss but then he can’t process the first line of it which is @import "@material/button/mdc-button"; in this case.

Nothing seems working. Anyone would see what am I missing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
skatAn8rwpascommented, Jul 29, 2019

Follow the instractions of how to import the scss files from material.io

@import "@material/button/mdc-button";

.foo-button {
  @include mdc-button-ink-color(teal);
  @include mdc-states(teal);
}

And just include the node_modules directory. Thre is a “./” at the bigin.

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        includePaths: ["./node_modules"]
      }
    }
  }
}

delete the lines

import * as mdc from 'material-components-web';

Vue.use(mdc);

and lastly import components in your .vue files as in documentation

example Toolbar.vue

<template>
<header class="mdc-top-app-bar">Your TopAppBar staff...</header>
</template>

<script>
import {MDCTopAppBar} from '@material/top-app-bar';

export default {
  mounted() {
    MDCTopAppBar.attachTo(this.$el)
  }
}
</script>

<style lang="scss">
@import "@material/top-app-bar/mdc-top-app-bar";
</style>
0reactions
LightBencommented, Aug 19, 2019

@skatAn8rwpas It works! I’m so happy. I’m putting the summary of what is final in case other people need. Project with vue and material design web :

  1. npm install material-components-web
  2. In vue.config.js:
module.exports = {
  css: {
    loaderOptions: {
      sass: {
        includePaths: ["./node_modules"]
      }
    }
  }
}
  1. In src/app.scss: @import "~material-components-web/material-components-web";
  2. If someone wants the icons, add in public/index.html, in the <head>: <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  3. Use Material. Example of a button in my views/About.vue:
<button class="mdc-button">
  <span class="mdc-button__label">Button</span>
</button>

Thanks everybody!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue Cli 3 Typescript. Cannot import css/sass to the <script ...
Any idea what to put in vue.config.js to able to import css and sass files directly to block? I need to use typings-for-css-modules-loader ......
Read more >
Working with CSS - Vue CLI
vue files out of the box with <style module> . To import CSS or other pre-processor files as CSS Modules in JavaScript, the...
Read more >
My SCSS setup within a Vue CLI 3 project - DEV Community ‍ ‍
Go to vue.config.js and add the following code, replacing the stylesheet name/path with whatever you have named your scss file for your global ......
Read more >
Sass modules with the Vue CLI - Ivancic Josip
Not enough people are aware of the announcement of the new Sass module system, of which the primary aim is to replace the...
Read more >
Vue - Mixing SASS with SCSS, with Vuetify as an Example
What is happening is that vue-loader/sass-loader is essentially trying to inject your import code, which is formatted as SCSS syntax (with a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found