5

I created a new react app by Create-React-App and I tried to use CSS Modules, but it's doesn't seem to work anymore. here is my simple usage of CSS modules :

import styles from './myStyle.css';

function App() {
  return (
    <div className={styles.container}>
    </div>
  );
}

It's work on my older project, the only differences in my older project with the new one is the react-scripts version.

my new project dependencies :

 "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.0"
  }

my old project dependencies :

 "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "2.1.8"
  },

Has anything changed in react-scripts v3 ?

2
  • Can you elaborate the "not working" concept? Are you getting an error? The app works but you don't see any style? Commented Apr 25, 2019 at 10:05
  • There's no error, but the values aren't resolved. Instead, the variable name remains in the actual CSS. Since it's of course an invalid value, the styles are just ignored. Commented Feb 29, 2020 at 23:13

1 Answer 1

9

To use CSS module, you should name your file to [name].module.css:

This project supports CSS Modules alongside regular stylesheets using the [name].module.css file naming convention. CSS Modules allows the scoping of CSS by automatically creating a unique classname of the format [filename]\_[classname]\_\_[hash].

https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet

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

2 Comments

The fact this is necessary is a regression bug in CRA 3.0.0. See github.com/facebook/create-react-app/issues/7306
and normally it must be done using import styles from 'fileName.module.css' and then use the styles object to call sub classes names : className={{styles.className}}

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.