Error encountered in Webpack when attempting to import SCSS into another SCSS file

I'm currently facing issues with Webpack (version 3.6.0) while trying to combine multiple SCSS files into a single external CSS file due to problems with parsing the @import statements.

The entry index.js file includes:

import './styles/main.scss';

Here is an example of the entry SCSS file:

@import 'reset.scss';
@import 'global.scss';
@import 'fonts.scss';
@import 'system.scss';

This is the current webpack configuration being used:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: {
        app: './src/index.js'
    },
    context: path.resolve(__dirname),

    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'bundle.js',
    },

    plugins: [
        new CleanWebpackPlugin(['app']),
        new HtmlWebpackPlugin({
            title: 'Minimum-Viable',
            filename: 'index.html',
            template: './public/index.html',
        }),
    ],

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            'es2015',
                            'react',
                        ],
                        plugins: ['transform-class-properties'],
                    },
                },
            },
            {
                test: /\.scss$/,
                include: [
                    path.resolve(__dirname, 'styles')
                ],
                use: [
                    {loader:'style-loader'},
                    {loader:'css-loader'},
                    {loader:'sass-loader'}
                ]
            },
        ],
    },
    resolve: {
        extensions: ['.js','.scss']
    }
};

An error is being thrown stating:

ERROR in ./src/styles/main.scss Module parse failed: Unexpected character '@' (1:0)

You may need an appropriate loader to handle this file type. | @import 'reset.scss';

If anyone has any helpful tips or pointers on how to resolve this issue, it would be much appreciated.

Answer №1

I successfully reproduced the issue and it appears to be stemming from:

include: [
    path.resolve(__dirname, 'styles')
],

The problem lies in the path because the loader is searching for /styles in ./styles, but based on your entry point:

entry: {
    app: './src/index.js'
},

It should actually be ./src/styles. Therefore, the corrected code should be:

include: [
    path.resolve(__dirname, 'src', 'styles')
],

Answer №2

My configuration is very similar to yours, with just a couple of differences:

{
  test: /\.scss$/,
  exclude: /node_modules/,
  use: ["style-loader", "css-loader", "sass-loader"]
}

If your webpack setup is not in the same directory as your project root, using `__dirname` might cause issues for you. You may need to consider removing that line or replacing it with `process.cwd()`. Also check if you have installed the necessary `node-sass` package. Additionally, make sure you are using the correct syntax for webpack version 2 or 3 in the `use` key.

Answer №3

when using:

include: [
    path.resolve(__dirname, 'styles')
],

webpack will only apply your loader chain to files located in the styles directory.

If you have a file like reset.scss that is a node dependency typically found in the node_module, it will not be processed by SASS due to the exclusion set by the include option.

To resolve this, consider either removing the SASS include option entirely or expanding it to encompass the node_folder or any specific modules being imported by your styles.

Answer №4

If you haven't tested out the Textify plugin for Webpack, give it a go!

https://github.com/webpack-contrib/textify-webpack-plugin

This amazing tool gathers all your imported style files in your code and compiles them into one bundled CSS file that can easily be linked to your HTML.

Answer №5

Have you attempted this approach?

index.js:

import './styles/main.scss';
import './styles/reset.scss';
import './styles/global.scss';
import './styles/fonts.scss';
import './styles/system.scss';

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

Is there a way to align an image to the center using the display grid property?

Having some trouble centering an image within a 'display: grid' layout. Screenshot I attempted to use 'align-items' but it didn't seem to work either. Code: <section style="padding-top: 30px;"> <h4 sty ...

The module could not be found: Issue arises when trying to require a CSS file within a directive but the file or directory cannot be resolved

Following an angular+webpack tutorial, a new folder named "directives" was created. Inside this folder, a custom directive called 'kcdHello' was declared as shown below: export default ngModule => { ngModule.directive('kcdHello' ...

There seems to be a display issue with the DataTables tables

Utilizing Bootstrap 4, I followed the example provided by DataTables on their website: link. The specific code that was implemented can be found here: link Take a look at how it appears: http://pastebin.com/HxSNUnLq Any suggestions on how to resolve th ...

Gallery not responsive on HTML/CSS site

Hi there, I'm a newcomer to Stack Overflow and currently diving into the world of HTML/CSS. I've hit a roadblock with my latest project - I'm trying to create a gallery of album covers, but when I try to 'zoom in' on the webpage (n ...

A Firefox Browser View of a Next.js and Tailwind Website magnified for closer inspection

After creating a website using Next.js and Tailwind CSS, I noticed that the site is appearing zoomed in when viewed in Firefox. However, it looks fine in all other browsers. When I adjust the screen to 80%, the website displays correctly in Firefox. What ...

Every time I execute npm start, the specified main output file fails to generate

While developing a React app with webpack, I encountered an issue where running npm start yielded a success message without any errors. However, the main output file that was supposed to be created was missing. In my webpack.config.js file, I have configu ...

Hovering over a thumbnail image triggers a popup in CSS3 or Jquery

Looking to showcase a collection of thumbnail images on a webpage with specific height and width dimensions? Wanting to implement a hover effect that displays a larger image in a fixed position within the wrapper at coordinates top:50px and left:50px? The ...

The color attribute for the ion-button element is not functioning properly on Android devices in

In my app, it functions correctly when running on iOS, but encounters issues when running on Android. The problem lies in the color attribute not working on buttons and other elements on Android. Removing the attribute makes them visible, but the desired s ...

Tips for eliminating the bottom border on a nav-tab in Bootstrap 4

When a vertical navigation menu item is selected, a border appears below the item. How can I remove this border? .nav-tabs li, .nav-tabs li a { border-bottom: none; } Here is a picture of the border: ...

What is the best way to incorporate regular, light, and bold fonts into your project using links from Google Fonts

I am looking for three specific styles of Ubuntu font without having to download them. To access these fonts, I inserted this link in the <link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,700" rel="stylesheet"> According to Google& ...

Conceal a division using CSS based on its data-role attribute

After extensive research, I have yet to find a solution. Let's say there is a div structured like this: div[data-role="form-footer"] What would be the most effective method for hiding it, and can it be done using CSS? I've tried the f ...

What is the correlation between using em font sizes and disrupting line-height?

I initially set my body element's font-size to 19px, and then started using em units for various elements. Unfortunately, I am experiencing irregularities with line-heights. For example, when I use a font-size of 0.6em, the line height becomes uneven ...

The height of the iframe with the id 'iframe' is not functioning as expected

I am trying to set the iFrame's height to 80% and have an advertisement take up the remaining 20%. Code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <iframe id="iframe" src="xxxxxx" style="border: ...

Ways to expand the div to fit the width of the text after it has been wrapped

I need a continuous underline to stretch across the remaining width after the text wraps in a table cell. Here is the desired effect: https://i.sstatic.net/XNNyj.png If the text wrapping changes, the underline width should adjust accordingly. This is w ...

Protecting against overflow for text inside a container that changes when hovered over

There is text displayed on top of an image within a div container that functions as a link. <a href="https://link.com" style="color:white"> <div class="thumbnail"> <img src="image.jpg" clas ...

Making the text gradually disappear at the bottom of a section using a see-through overlay, while ensuring the height remains within the boundaries of the section even

Trying to achieve a sleek fade-out effect at the end of a text section for a 'read more' indicator. I've been studying various tutorials, including this, and my current code is as follows: html <section> <p>Lorem ipsum dol ...

What is the process for choosing a particular input element based on its size and type?

Among my pair of sets containing input type=text elements, each group is uniquely styled through CSS. In the first set, the length adjusts based on CSS using the width property. Conversely, in the second set, the size attribute determines the length of the ...

Is there a way to eliminate the hover effect on a button within a navbar?

I'm currently working on a website using Angular and I have implemented a gray navbar with a hamburger icon that reveals a dropdown menu upon clicking. One of the menu items is "Projects," which when hovered over, displays individual links to various ...

When I resize the screen size, my multiple item carousel in Bootstrap is failing to adjust and collapses

Whenever I try to resize the output screen, the carousel collapses and the last few items disappear. I really need the carousel to stay intact even when resizing. Can someone please assist me with this issue? Link to my CodePen ResCarouselSize(); $(w ...

Interactive hover effect in JavaScript displays a larger version of other thumbnails when hovering over a dynamically loaded thumbnail image, instead of its own full-size image

I recently began teaching myself PHP and Dreamweaver with the help of a video tutorial on building data-driven websites using Dreamweaver. My goal is to create a dynamic table with 6 columns and 20 rows. column1 | column2 | column3 | colu ...