How to use Sass and CSS Modules with create-react-app?

11,740

You will have to install node-sass in order to work with scss files.

npm install node-sass --save-dev

Your code seems to be alright as post React 16.8 you can use css and scss modules without configuring webpack. I would suggest you check your version of React first. If you are using a version of React < 16.8 then you would have to eject and configure your webpack in order to use css and scss modules.

Share:
11,740

Related videos on Youtube

Lamis Abouzina
Author by

Lamis Abouzina

FrontEnd web developer

Updated on June 04, 2022

Comments

  • Lamis Abouzina
    Lamis Abouzina almost 2 years

    I am using FileName.module.scss to style my react elements like so:

    // this is my component
    import React from "react";
    
    import Aux from '../../hoc/Aux';
    import classes from './Layout.module.scss';
    
    const layout = (props) => (
        <Aux>
            <div>Toolbar, SideDrawer, Backdrop</div>
            <main className={classes.Content}>
                {props.children}
            </main>
        </Aux>
    );
    
    export default layout;
    

    This is my SCSS:

    .Content {
        margin-top: 72px;
        color:red;
    }
    

    I don't know why but the scss is not being applied to my main element, any ideas? Thank you!

  • Marceli Wac
    Marceli Wac over 2 years
    This can create the problems in the form of variable shadowing (for example, if you had button defined in your component, and the styles defined for class called .button in your scss).
  • Sri
    Sri about 2 years
    node-sass has been deprecated. Use the dart sass instead - npm install sass
  • Brent
    Brent about 2 years
    This is interesting. I had no idea.