10

I'm trying to add sass/scss support to create-react-app.

So I did these steps:

  1. npm eject
  2. npm install sass-loader node-sass --save-dev
  3. Opned the webpack.config.dev.js file and added this to loaders section:

    {
        test: /\.scss$/,
        loaders: ["style", "css", "sass"]
    },
    

In my app.js file i reference an scss file: import './css/app.scss';

When i run npm start everything compiles with no errors but the application loads with no styling.

What am i missing?

5
  • you missed include: paths.appSrc Commented Dec 8, 2016 at 15:02
  • @TheReason - I tried with and without it, didn't help either way Commented Dec 8, 2016 at 15:04
  • 5
    Did you follow this comment and add a line to exclude? Commented Dec 8, 2016 at 17:03
  • @DanAbramov - Thanks Dan, it worked! How about you write it as an answer so i can mark it as true? Commented Dec 8, 2016 at 21:17
  • I had this issue coming from an older version of react-scripts using node-sass-chokidar. I was able to get it working by deleting the old autogenerated .css files and uninstalling node-sass-chokidar and npm-run-all. After, I ran npm install sass-loader node-sass --save-dev. Then, I ran npm start and it output file path errors in the console, which I was able to resolve quickly with using the CRA style docs. Then, it all worked fine. Commented Dec 10, 2019 at 14:30

5 Answers 5

16

I just went through this, and seems that there are lot of people lost or not finding the right answer. This is what I did:

Get control over the configuration

npm run eject

Running this command will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project, as you noted all that was inside a npm module called react-script. Do not modify this package directly!

Install sass compiler and webpack loader

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

At this point you have to install the sass compiler and the webpack loader as development dependencies.

Modify the webpack configuration

  1. Open config\webpack.config.dev.js.
  2. Add /\.scss$/, to the exclude array in the url loader, it will look something like this after the update:

    exclude: [
        /\.html$/,
        /\.(js|jsx)$/,
        /\.css$/,
        /\.json$/,
        /\.svg$/,
        /\.scss$/, //Add this line
    ],
    
  3. Add the SASS/SCSS loader:

    {
        test: /\.scss$/,
        loaders: ['style', 'css', 'sass']
    },
    
Sign up to request clarification or add additional context in comments.

5 Comments

@BrunoQuaresma, maybe if you share a more detailed explanation I could help :)
I reseted the file, followed the instructions again and it worked. Thanks!
You can add sourceMap support: loaders: ['style', 'css?sourceMap', 'postcss' ,'sass?sourceMap']
at development this makes load sass files in <style> tags and build will not include styles at all.
3

I was stuck with the same problem reading it's documentations.

Unitl I found that the README.md file inside the created project with create-react-app has documented it in another way and it worked. I think relying on that readme file ( that is shipped with your created project ) is better option.

Comments

1

Styling using Sass without Ejecting

1) Install Koala

Koala is a sass, less etc.. compiler. You can use any tools of your choice for this.

2) Add your src folder

Add the source folder containing all your SCSS files to Koala. It will monitor for any modification to your Sass files and generate the compiled CSS version instantly.

3) Refer to the CSS files

Use the CSS file generated by Koala directly your project.

import './style.css';

Comments

1

Step 1:

npm run eject

Now the config files will be shown in the project folder

Step 2: Install sass compiler and webpack loader

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

In config\webpack.config.dev.js add /.scss$/ to the exclude array

Step 3: Add below loader in the rules array (should be added before file-loader)

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

And start your application.

You can rename your existing css files to scss and work

1 Comment

never eject - use customize cra etc
-1

{
    test: /\.scss$/,
    loaders: ['style', 'css', 'sass','scss']   //add 'scss'
},

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.