darkkenergy
darkkenergy

Reputation: 75

Webpack, sass-loader w/ node-sass fails to parse module @import ("Unexpected character '@'")

Overview: I'm trying to compile SCSS using Webpack with "node-sass", "sass-loader", "css-loader", & "style-loader" and no particular JS framework, aside from Typescript as the JS transpiler.

I've tried everything I can think of or could find on the internet, including installing older versions of these dependencies that I know are working in other projects. At this point, I'm at my wits end. Can anyone spot what I'm doing wrong?

Please let me know if I need to add any more details.

Observed error:

ERROR in ./src/styles/base.scss 1:0

Module parse failed: Unexpected character '@' (1:0)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders @import 'reset';

|

| /*

@ ./src/layouts/page.ts 21:0-30

@ ./app.ts

ℹ 「wdm」: Failed to compile.

package.json details:

Webpack config:

const path = require('path');
const CWD = process.cwd();
const sass = require('node-sass');
module.exports = {
    devtool: 'eval-source-map',
    devServer: {
        port: 4001
    },
    entry: {
        app: './app.ts'
    },
    output: {
        chunkFilename: '[id].js',
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js',
    },
    mode: 'development',
    module: {
        rules: [{
            exclude: /node_modules/,
            test: /\.ts$/,
            loader: 'ts-loader'
        },
        {
            test: '/\.(css|scss)$/',
            use: [
                // Creates `style` nodes from JS strings
                'style-loader',
                {
                    // Translates CSS into CommonJS
                    loader: 'css-loader',
                    options: {
                        modules: true
                    }
                },
                {
                    // Compiles Sass to CSS
                    loader: 'sass-loader',
                    options: {
                        sassOptions: {
                            includePaths: [path.resolve(CWD, 'src/styles')]
                        }
                    }
                }
            ]
        }]
    },
    // Fixes npm packages that depend on `fs` module
    node: {
        fs: 'empty'
    },
    resolve: {
        extensions: ['.css', '.js', '.scss', '.ts'],
        modules: [
            'node_modules',
            path.resolve(__dirname)
        ]
    },
    target: 'web'
};

Upvotes: 1

Views: 2532

Answers (1)

walston
walston

Reputation: 26

looks like you accidentally quoted your regex:

- test: '/\.(css|scss)$/',
+ test: /\.(css|scss)$/,

Upvotes: 1

Related Questions