Tutorial on implementing a _variables.scss file for Vue components with sass-resource-loader on Vue CLI 3.04

In my VueJs project created with the Vue CLI 3.0.4, I am aiming to utilize SCSS variables across all components without the need to import _variables.scss into each one individually. After some research, I found that I can achieve this using sass-resource-loader. However, the existing solutions I came across are outdated and not compatible with vue-loader 15.

To address this issue, I have the following setup inside vue.config.js:

var path = require('path');
module.exports = {
  chainWebpack: (config) => {
    config.module
      .rule('sass')
      .use('sass-resources-loader')
      .loader('sass-resources-loader')
      .options({
        resources: [
          path.resolve('./src/scss/config/_variables.scss'),
        ]
      })
  }
}

Unfortunately, when running this configuration, I encounter the following error:

Module build failed (from ./node_modules/sass-loader/lib/loader.js):

undefined
        ^
      Undefined variable: "$brand-color".
      in C:\dev\git\vue-typescript-test\src\components\HelloWorld.vue (line 60, column 10)

The $brand-color variable is utilized within the HelloWorld.vue component, indicating that the variables are not being properly included.

I am puzzled by why this setup is not working as expected, considering that I followed the Vue CLI documentation diligently.

To tackle this problem, I referenced a particular answer on StackOverflow at Vue CLI 3 sass-resources-loader - Options.loaders undefined.

Any assistance on this matter would be immensely appreciated.

Thank you!

Answer №1

To ensure proper functionality, utilize the code below.

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/scss/config/_variables.scss";`
      }
    }
  }
};

Remember that @ is a quick way to access the src folder.


Remember to also install node-sass and sass-loader.

npm install --save-dev node-sass sass-loader


Lastly, include the lang attribute in your style tags.

<style lang="scss">

// some style...

</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

VueRouter - The local storage data will be wiped out if a trailing slash is not included in the URL

Storing a JWT token in localStorage has caused an issue where the token is only visible in Developer Tools when visiting https://www.example.com/my-web-app/. However, when visiting https://www.example.com/my-web-app, the data in localStorage remains invisi ...

Pinia store encountering a Typescript Vue component issue: error message "The property 'testStore' does not exist on the 'CreateComponentPublicInstance' type."

Within my <script setup> block, I have imported my testStore. However, whenever I attempt to use this.testStore.name in my Vue file, Vetur displays the following error: Property 'testStore' does not exist on type 'CreateComponentPublic ...

"Encountering an error while parsing the result of an HTTP request in Node.js

Receiving information from an external api: var requestData = http.get('http:...format=json', function(responseFromApi) { if(responseFromApi.statusCode !== 200){ res.status(400).send(data); return; } responseFromApi.on('data&a ...

Update the network name in the Axios request

Currently, I am utilizing Axios for handling both GET and POST requests. One question that has been on my mind is how to modify the network name: At present, the name serves as the parameter accompanying each request. However, I ponder whether it's f ...

Having trouble launching the freshly developed Angular app

I'm encountering an issue with my newly created app - I can't seem to launch it. Error: The loader “C:/C#/Angular/my-app/src/app/app.component.css” is not providing a string as expected. https://i.sstatic.net/6Xjwd.png https://i.sstatic.ne ...

Stopping the infinite refresh issue in your React webpack application

Every time I modify the TS file, Webpack keeps refreshing the page without stopping. The console message reads: "@ebpack 5.66.0 compiled successfully" I've searched online and experimented with various plugins, but none of them seem to solve the issu ...

I am struggling with utilizing the data that has been fetched in JavaScript. Despite successfully retrieving the data, I am facing difficulties in effectively using it in

I am facing an issue where I am unable to utilize the data after fetching it from a local JSON file. Below are the screenshots showcasing the problem: Here is the data displayed in the console: Here is the code snippet: fetch('api_link.json') ...

Initiating PHP outcomes with the integration of JQUERY and Bootstrap

Currently, I am working on a Twitter search functionality that allows me to search for any content on Twitter. While the feature is functional, I am facing some challenges with displaying the results in the desired format. Ideally, I would like the results ...

Integrate the complete Mozilla pdf.js viewer into a Vue.js application using webpack through vue-cli

I am trying to integrate the full Mozilla pdf.js viewer into a Vue SPA. After reading a Stack Overflow post with an accepted solution, I still can't seem to get it to work. I keep encountering the 'Uncaught SyntaxError: Unexpected token <&apo ...

The Thinkster.io MEAN Stack guide: A blank "post" appears on the homepage. What is causing this and how can I remove it?

Currently, I am immersed in the MEAN Stack tutorial provided by Thinkster.io. At this stage, I am integrating the node.js backend with the Angularjs frontend. The functionality includes data persistence for users to add posts and upvote them. However, an ...

Leveraging the power of Firebase and JavaScript to easily include custom user fields during the user

UPDATE: I encountered an issue while using Nextjs framework. However, it works perfectly fine when I use a vanilla CRA (Create React App). It appears that the problem is somehow related to Nextjs. I'm currently working on creating a new user document ...

Creating app.css from app.sass with Laravel

UPDATE: Currently operating on Laravel 5.4.30 with Windows 10 UPDATE: Despite .scss files working efficiently, I am inclined to use indented sass or .sass instead for my project. After dedicating countless hours to research, I have finally decided to see ...

"Server request with ajax did not yield a response in JSON format

http://jsfiddle.net/0cp2v9od/ Can anyone help me figure out what's wrong with my code? I'm unable to see my data in console.log, even though the network tab in Chrome shows that my data has been successfully retrieved. Here is my code snippet: ...

Steps for displaying nested JSON data in VueJS:

After diving into VueJS, I decided to create a table using a JSON file. Although I successfully generated the table, I'm facing an issue with removing three empty rows located above the actual data. Any suggestions on how I can resolve this? https:/ ...

Can you explain the significance of syntax in sample code (typescript, react)?

const sampleFunction: (inputString: string) => string = inputString => { return inputString.split(""); } I'm a bit confused about the code below and would appreciate some clarification. I understand that only "string" as a type is accepted, b ...

What could be causing the service method in the controller not to be called by Node JS?

In my current Node JS project, the folder structure of my app is as follows: src │ index.js # Main entry point for application └───config # Contains application environment variables and secrets └───controllers # Hou ...

Discovering the indices of undefined array elements in JavaScript without using a loop

Is there a way in JavaScript to retrieve the indexes of undefined array elements without using a loop? Possibly utilizing a combination of map, filter, and indexOf? I have a loop solution that I'm seeking an alternative for - something more concise, ...

Eliminate the classes that were inserted through Jquery

I'm currently working on an accordion and I've encountered an issue with jQuery adding classes that I don't want. How can I prevent jQuery from adding certain classes? The code below is what I have, but for some reason, jQuery keeps adding t ...

Best practices for implementing localization in AngularJS 1.5.x

Visit this link When working with newer versions of the framework (>=1.4.0), it is recommended to utilize the built-in i18n tools. For older versions (<1.4.0), angular-translate is a suitable option. The documentation provides detailed steps for ...

Utilize Node Package to Dispatch Firebase Push Notifications

I am currently experimenting with a NodeJS module for sending Push Notifications. I have been exploring the 'node-sender' module but have not been able to find an option for sending notifications to a web browser for testing purposes: https://w ...