Encountered an error while building CSS Modules for a Vue.js project

Working on a project in Vue.js, I decided to incorporate CSS modules for my local styles (for sass and scss). However, I continuously encounter a 'Failed to compile error' related to the CSS Loader validation process.

Despite attempting various solutions from Github and configuring my Webpack setup in different ways, the error persists.

Below is an excerpt from my webpack.config.js file within module.rules:


{
    test: /\.scss$/,
    use: [
      'vue-style-loader',
      {
        loader: "css-loader",
        options: {
          modules: {
            localIdentName: 'MyApp__[local]--[hash:base64:5]',
          },
        }
      },
      'sass-loader'
    ],
  },
  {
    test: /\.sass$/,
    use: [
      'vue-style-loader',
      {
        loader: "css-loader",
        options: {
          modules: {
            localIdentName: 'MyApp__[local]--[hash:base64:5]',
          }
        }
      },
      'sass-loader?indentedSyntax'
    ],
  },

Furthermore, here is how I implement CSS modules on a child component in Vue:

<style lang="scss" module>
.subtitle{
    font-size: 1rem !important;
}
.box{
    padding: 20% 0 !important;
    box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.20);
}
</style>

The specific error message that I am encountering is as follows:

ERROR in ./node_modules/css-loader/dist/cjs.js?{"localIdentName":"[hash:base64]_0","importLoaders":true,"modules":true}!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-68c39e04","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!
Module build failed: ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
 - options has an unknown property 'localIdentName'. These properties are valid:
   object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals? }
    at validate

I remain hopeful in finding a resolution to this issue.

Answer №1

Regrettably, the getLocalIdent option provided by css-loader does not seem to function properly when used with CSS processed through vue-loader.

In a statement made by the creator of Vue.js, Evan You, it was noted:

"This issue arises because the query for vue-loader loaders needs to be converted into a string form, causing the exclusion of any function options."

If you are looking for a potential workaround solution, you may find this article helpful: https://medium.com/@my_own_grave/reducing-css-generated-by-vue-loader-by-using-classnames-shorten-trick-aa1d25d77473

(!) An alternative solution that I have recently discovered and successfully implemented in our project involves utilizing Webpack 4. By passing localIdentName as an object within modules, you can easily achieve this configuration:

use: [
          'vue-style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: { localIdentName: '[name]_[local]_[hash:base64:5]' }
            }
          },
          'sass-loader'
        ]

Answer №2

Based on the documentation provided, the correct syntax is:

  options: {
    modules: true,
    localIdentName: 'MyApp__[local]--[hash:base64:5]'
  }

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

Learn how to implement console-like Tail functionality in a web application using the MaterialUI TextField component as the console interface

Issue with Component: Scroll not Updating as new content is added to TextField: I am currently in the process of developing a live console feature within a React application and I wanted to utilize MaterialUI's TextField component since my app alread ...

What is the recommended way of inserting new data into a table in vue.js?

Is it possible to update the table data whenever I want, even after initializing the Vue instances? I've attempted to use the following methods but the data is still not updating: ・$nextTick ・$set <script> var overall = new Vue({ ...

Is it possible for JavaScript to be cached when it is located within the body tag of an HTML document?

Currently, I am exploring the topic of How to optimize HTML rendering speed, and came across the idea that scripts in the HEAD tag can be cached. I'm curious, is it possible to cache JavaScript in the BODY tag? If not, I wonder why YUI suggests placi ...

What is the best way to associate an array of objects with another array in React or JavaScript?

const arrayObj = [{ id: 123, country: "IND" value: "" }, { id: 153, country: "AUS" value: "" }, { id: 183, country: "FRA" ...

Once a WordPress user includes a php file

For the past three days, I've been grappling with this issue... calling on all experts for assistance! I manage four distinct Wordpress membership sites, each with its own branding. My goal is simple - to have a .wav file play the plan's name wh ...

Issue: Reactjs - Material-UI HTML Tooltip does not display dynamic HTML content.In the Reactjs

I have been using a customized HTML MUI Tooltip. Currently, it is functioning with static content but I am looking to make it dynamic. Unfortunately, it is not working with dynamic HTML content. Here is my attempted approach: const ADJUSTMENT_HELP_TEXT = ...

What is the best way to consistently and frequently invoke a REST API in Angular 8 using RxJS?

I have developed a REST API that retrieves a list of values. My goal is to immediately invoke this API to fetch values and store them in a component's member variable. Subsequently, I plan to refresh the data every five minutes. Upon conducting some ...

Incorporating traditional Bootstrap styling directly into a React application without relying on the react-bootstrap library

My goal is to incorporate my existing bootstrap components into a React project without having to rewrite them to work with the react-bootstrap library. While I have successfully integrated the bootstrap CSS, I am facing issues with the functionality aspec ...

Having trouble with the downloadUrl function when trying to generate a Google map locator in Opencart?

Currently, I am working on the development of a website using opencart at Within the dealers.php file, there is a function named query() which searches postcodes in the database to retrieve store location results and outputs them in XML format. For exampl ...

Changing state property upon clicking child element in React is not feasible

I am interested in changing the text color upon clicking it using a function triggered in a child element that affects the parent's state. This is because a specific property of the parent determines whether the text should be one color or another. W ...

Resolve React Issue: Using Functions as React Children is Invalid

As I follow along with a React tutorial, I encountered an error while adding a POST request: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than ...

What is the best way to keep the navbar contained within the parent div (hero image) no matter the size of the screen?

After working on adjusting my website layout for different screen sizes, I encountered an issue with the navbar overflowing the parent image when viewed on my laptop. Despite trying both relative and absolute positioning for the .navbar element and setting ...

Tips for moving an element to the end of an array

Patients' data is stored in the MongoDB database, and all patients are mapped through on the frontend to display a list. An additional boolean value indicates whether a patient is archived or not. If a patient is archived, it should be displayed at th ...

Is it possible to resize an object using JavaScript?

Is it possible to change the size of an object when loading specific data by clicking on navigation? <object id="box" width="250" height="250" data=""></object> Although the JS code loads the data, it does not change the size. document.getEl ...

Tips and tricks for selecting a specific element on a webpage using jQuery

How can I modify my AJAX form submission so that only the content within a span tag with the id "message" is alerted, instead of the entire response page? $.ajax({ url: '/accounts/login/', data: $(this).serialize(), success: function(ou ...

TS2307 Error: The specified module (external, private module) could not be located

I have come across similar queries, such as tsc throws `TS2307: Cannot find module` for a local file . In my case, I am dealing with a private external module hosted on a local git server and successfully including it in my application. PhpStorm is able ...

Using the spread operator in a component's render function could potentially lead to an endless update loop

Although this issue has been addressed before in a discussion about the "You may have an infinite update loop in a component render function" warning in Vue component, the solution provided did not resolve my problem. I am seeking assistance to ...

NextJS does not support the rendering of the map function

Currently, I am getting acquainted with NextJS by creating a basic blog. I have successfully passed the data through props and can see it logged in the console within the map function. However, I am facing an issue where the HTML content does not display i ...

Connecting a string array to the navigation bar

Need Assistance with AngularJS: In my controller, I have a string array that looks like this: var app = angular.module('myApp', []); app.controller('mycontroller', function($scope) { $scope.menuitems =['Home','About&apos ...

Turn off a feature

I'm having trouble disabling a tooltip that is being utilized from this specific website. The code for setting and calling the tooltip looks like this: this.tooltip = function(){....} $(document).ready(function(){ tooltip(); }); I've attempte ...