5

I am trying to setup my react project so I can use SASS in the SCSS format.

This is in my webpack.config.dev.js:

      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: [
          require.resolve('style-loader'),
          {
            loader: require.resolve('css-loader'),
            options: {
              importLoaders: 1,
            },
          },
          {
            loader: require.resolve('sass-loader'),
          }
        ]
      }

I import the scss files into my jsx in two different ways:

import './index.scss';
import css from './ModalWrapper.scss';

When I run the app I am currently getting the error:

./src/index.scss
Module build failed: 
body {
^
      Invalid CSS after "m": expected 1 selector or at-rule, was "module.exports = __"
      in /pathtoapp/web/src/index.scss (line 1, column 1)

It appears me, that one, react is trying to interpret the SCSS as CSS which should work. In addition, react believes that body is not valid CSS. There, I would believe that neither CSS or SCSS are being loaded correctly.

Any help would be appreciated. There are quite a few unanswered questions to this problem.

5
  • Are you importing each scss file into a different component for - component styles, or do you just import the root scss file in your root app file? Commented Nov 7, 2017 at 21:06
  • I import the scss file into a different component for component styles. However, I would also like to have a couple global scss files for constants and fonts. Commented Nov 7, 2017 at 21:11
  • Well its a little more complex than a simple answer, but I think this article will help guide you through installing and getting it setup -> medium.com/@srinisoundar/… Commented Nov 7, 2017 at 21:19
  • Why are you require.resolve on the loaders? Just do loader: 'saas-loader' and similar for other loaders in your config above. Commented Nov 8, 2017 at 16:28
  • I've tried that as well @VishalMalik Commented Nov 8, 2017 at 18:20

3 Answers 3

19

If you are on Webpack 3, add this to module.rules

{
    test: /\.scss$/,
    loader: [
      require.resolve('style-loader'),
      require.resolve('css-loader'),
      require.resolve('sass-loader'),
    ]
},

And then add the file loader and make sure to add .scss to the array of the key exclude like this

{
        loader: require.resolve('file-loader'),
        // Exclude `js` files to keep "css" loader working as it injects
        // it's runtime that would otherwise processed through "file" loader.
        // Also exclude `html` and `json` extensions so they get processed
        // by webpacks internal loaders.
        exclude: [/\.js$/, /\.html$/, /\.json$/, /\.scss$/,],
        options: {
          name: 'static/media/[name].[hash:8].[ext]',
        },
}

And of course, make sure you have style-loader, sass-loader, css-loader and file-loader in you package.json. This code snippet worked for me when using the latest version of create-react-app.

Sign up to request clarification or add additional context in comments.

1 Comment

Saver~! I spent 2 hours on finding out what's wrong in my webpack config and this one solved my problem~! thanks~!
0

This is what ended up working for me:

{
        test: /\.scss$/,
        use: [{
          loader: 'style-loader'
        }, {
          loader: 'css-loader',
          options: {
            modules: true,
            sourceMap: true,
            localIdentName: "[local]___[hash:base64:5]",
          },
        }, {
          loader: 'sass-loader',
          options: {
            outputStyle: "expanded",
            sourceMap: true,
          },
        }]
      },

Comments

0

I'm sure the other answers are just as good, but for maximum brevity, this works for me (I erased some of the internal webpack.config.dev.js comments presumably made by the create react folks):

module: {
strictExportPresence: true,
rules: [
        {
            test: /\.scss/,
            use: ['style-loader','css-loader', 'sass-loader']
        },...

I don't know if it matters, but I put this on the top. Also, yes, make sure to add the scss file to the excluded array as mentioned above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.