You do not need to eject.

Create React App supports CSS Modules right out of the box as of version 2.

Upgrade to the latest (react-scripts@latest) version.

If you use yarn:

Copyyarn upgrade react-scripts@latest

If you use npm:

Copynpm install --save react-scripts@latest

Then you just have to create a file with the extension .module.css

For example:

Copy.myStyle {
  color: #fff
}

Then you can use it like so:

Copyimport React from 'react'
import styles from './mycssmodule.module.css'

export default () => <div className={styles.myStyle}>We are styled!</div>
Answer from giraffesyo on Stack Overflow
Top answer
1 of 11
126

You do not need to eject.

Create React App supports CSS Modules right out of the box as of version 2.

Upgrade to the latest (react-scripts@latest) version.

If you use yarn:

Copyyarn upgrade react-scripts@latest

If you use npm:

Copynpm install --save react-scripts@latest

Then you just have to create a file with the extension .module.css

For example:

Copy.myStyle {
  color: #fff
}

Then you can use it like so:

Copyimport React from 'react'
import styles from './mycssmodule.module.css'

export default () => <div className={styles.myStyle}>We are styled!</div>
2 of 11
11

To enable CSS module in to your app, you don't need to eject create-react-app. You can follow these simple steps described in this link to use CSS module in your project.

But in case if you ejected your create-react-app, then you must find file named

"webpack.config.js"

open this file and find this {test:cssRegex....etc} in line no. 391 and replace to this changes:

Copy            {
              test: cssRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
              importLoaders: 1,
              modules: true,
              localIdentName: '[name]__[local]__[hash:base64:5]'
            }),
            },

Open .js file and import statement like this

Copyimport cssStyleClassName from './MyApp.css';

Then this import statement allow you to do following changes in component tags like this

Copy<div className={cssStyleClassName.styleName}></div>

styleName is what you defined in your MyAppStyle.css file

Copy.styleName{
your properties here...
}

After eject your app, you must restart your local server, sometime changes don't get reflect.

Note In earlier version there were two generated file for production and dev separately. Now in current version one file named "webpack.config.js" file for which you need to concerned for changes. Thank you.

🌐
Create React App
create-react-app.dev › docs › adding-a-css-modules-stylesheet
Adding a CSS Modules Stylesheet | Create React App
January 4, 2019 - import React, { Component } from 'react'; import styles from './Button.module.css'; // Import css modules stylesheet as styles import './another-stylesheet.css'; // Import regular stylesheet class Button extends Component { render() { // reference as a js object return <button className={styles.error}>Error Button</button>; } } Copy ... <!-- This button has red background but not red text --> <button class="Button_error_ax7yz">Error Button</button> Copy
Discussions

In v5 can't use import * as styles with css modules, bug?
Hello, after upgrading to v5 I can not use anymore named import such as import * as styles from './Table.module.css'; This results in the following error export 'table' (imported as 'styles') was n... More on github.com
🌐 github.com
11
September 20, 2021
CSS class names with an hyphen(-) dont work in CSS Modules
I am using the latest build of Create React App with the latest versions of react, react-scripts etc. When i went ahead using the CSS Modules (by appending the .module.css to the css file) by havin... More on github.com
🌐 github.com
5
June 29, 2021
CSS module not working, maybe because of NewWindowComponent.js
Are you using webpack to create the module.css file? or it is manually created by you a named Blog.module.css? More on reddit.com
🌐 r/reactjs
7
0
January 21, 2021
reactjs - How do I fix create-react-app not loading CSS Modules? - Stack Overflow
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'; More on stackoverflow.com
🌐 stackoverflow.com
October 1, 2019
🌐
DEV Community
dev.to › tumee › how-to-use-css-modules-with-create-react-app-27eh
How To Use CSS Modules With create-react-app - DEV Community
June 5, 2020 - If you want create-react-app to treat a CSS file as CSS Module file, you need to use a specific naming convention when naming your CSS file. You need to name your CSS files with .module.css extension.
🌐
GitHub
github.com › facebook › create-react-app › issues › 11800
In v5 can't use import * as styles with css modules, bug? · Issue #11800 · facebook/create-react-app
September 20, 2021 - Hello, after upgrading to v5 I can not use anymore named import such as import * as styles from './Table.module.css'; This results in the following error export 'table' (imported as 'styles') was not found in './Table.module.css' (possib...
Author   mikoleg
🌐
GitHub
github.com › facebook › create-react-app › issues › 6142
[BUG] Css Modules don't seem to load with latest version of react-scripts (2.1.2) · Issue #6142 · facebook/create-react-app
January 7, 2019 - Environment Info: System: OS: Windows 10 CPU: x64 Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz Binaries: npm: 6.5.0 - *\node_modules.bin\npm.CMD (replaced path with *) Browsers: Edge: 42.17134.1.0 Internet Explorer: 11.0.17134.1 npmPackages: react: 16.7.0 => 16.7.0 react-dom: 16.7.0 => 16.7.0 react-scripts: ^2.1.2 => 2.1.2 npmGlobalPackages: create-react-app: Not Found · Update react-scripts to 2.1.2 · Create css file ( [name].module.css ) and import it (ex. import styles from './test.module.css';) Try using it in the return function (ex. className={styles.someTestClass}) Css styles to load; Css styles don't load at all. Had to revert back to 2.1.1. The text was updated successfully, but these errors were encountered: Copy link · Always use CSSmodules and works perfectly with the latest version of react-scripts: 2.1.3, just tried it again.
🌐
Robin Wieruch
robinwieruch.de › create-react-app-css-modules
How to use CSS Modules in create-react-app?
October 3, 2018 - After you have setup your application with create-react-app (e.g. npx create-react-app my-app), you don't need to install anything else to make CSS modules work.
🌐
Medium
medium.com › @ralph1786 › using-css-modules-in-react-app-c2079eadbb87
Using CSS Modules In React App. Welcome back reader to another blog… | by Rafael Cruz | Medium
February 23, 2022 - This blog will focus on what CSS Modules are and how you can use them in your React application (bootstrapped with Create-React-App). According to the official repo CSS Modules are “CSS files in which all class names and animation names are scoped locally by default”. What does this mean? CSS Modules are not specs or an implementation in the browser, but rather a process in a build step (with the help of Webpack or Browserify) that changes class names and selectors to be scoped (kinda like namespaced).
Find elsewhere
🌐
GitHub
github.com › facebook › create-react-app › issues › 11155
CSS class names with an hyphen(-) dont work in CSS Modules · Issue #11155 · facebook/create-react-app
June 29, 2021 - I am using the latest build of Create React App with the latest versions of react, react-scripts etc. When i went ahead using the CSS Modules (by appending the .module.css to the css file) by having a CSS class with the name Event-Entries. When I imported the css modules as import styles from './Event.module.css and then when i used it in my JSX like <div className={styles.Event-Entries}></div>, I got a compilation error saying "Entries is not defined".
Author   vikramuvs
🌐
OpenReplay
blog.openreplay.com › using-css-modules-in-react
Using CSS Modules in React
September 2, 2022 - The style will not be applied, for example, if you use random-gross-class in HTML without applying it as a CSS Modules-style class since the CSS selector will be changed style random-gross value. A very good point is that it is not necessary to configure WebPack: React handles everything for you when you install create-React-app; thus, you don’t currently need to configure Webpack for CSS Modules.
🌐
Bitstack
blog.bitsrc.io › how-to-use-sass-and-css-modules-with-create-react-app-83fa8b805e5e
How to Use SASS and CSS Modules with create-react-app | by Esau Silva | Bits and Pieces
March 4, 2025 - Up until the release of create-react-app v2, if you wanted to include Sass or CSS Modules in your project, you would have to eject from create-react-app, learn Webpack configurations, install Sass loaders and configure it yourself. ... Not anymore.
🌐
Reddit
reddit.com › r/reactjs › css module not working, maybe because of newwindowcomponent.js
r/reactjs on Reddit: CSS module not working, maybe because of NewWindowComponent.js
January 21, 2021 -

Right now I made another component that is on a new window called Blog.js. In-line styling is convenient but eventually it will get crowded and annoying to edit. I want to make another stylesheet using CSS module called Blog.module.css but when I style in it and import in Blog.js, it clashes with App.js component instead. Here's my code on stackoverflow: [https://stackoverflow.com/questions/69792331/how-do-i-make-a-new-css-file-for-a-new-component-in-react-js]

🌐
Robin Wieruch
robinwieruch.de › react-css-modules
How to use CSS Modules in React - Robin Wieruch
October 19, 2019 - That’s already it for the CSS Modules setup in Webpack. Next, you can define your first CSS file. Let’s name it src/style.css: ... Then you can import the CSS file in one of your React components. Afterward, you are already able to use the CSS class defined in the CSS file in your React component. Just import it and use its defined CSS class as className prop in your React component. ... import React from 'react'; import styles from './style.css'; const App = ({ title }) => ( <div className={styles.title}>{title}</div> ); export default App;
🌐
GitHub
github.com › facebook › create-react-app › issues › 7561
Hot reload is not working with CSS modules, starting in 3.1.0 · Issue #7561 · facebook/create-react-app
August 18, 2019 - System: OS: macOS 10.14.6 CPU: ... => 3.1.1 npmGlobalPackages: create-react-app: Not Found ... Try editing the css module file, it will reload the whole app....
Author   igorstajic
Top answer
1 of 2
2

If you intend to create a CSS module then use it in your components for scoping purposes, according to CRA you should define your CSS files with module extension.

When you don't use .module extension for your stylesheet files, you should import them like usual and you can't scope them as well.

So it will be something like this:

import "./BurgerIngredients.css";
... // other parts of your component
render(){
  return <div className="BreadBottom"></div>;
}

But if you want to use the CSS module and scope your styles, your stylesheet file should be BurgerIngredients.module.css instead of BurgerIngredients.css then you can import it without any scoping concern just like you did earlier.

import classes from "./BurgerIngredients.module.css";
... // other parts of your component
render(){
  return <div className={classes.BreadBottom}></div>;
}
2 of 2
2

The mistake you are doing here is your are calling your css with a name

Example:

import classes from "./BurgerIngredients.css";

And then you are using inline to append the class name like this

Example:

 <div className={classes.Meat}></div>;

Your css class names are not object so you cannot call it like this.

Instead all you have to do is just import the css like this (Make sure css file is in the same folder as the js file because here we are calling the file by ./ )

Example:

 import "./BurgerIngredients.css";

And use the className just like you would do in a normal html like this

Example:

 <div className="BreadBottom"></div>;
🌐
GitHub
github.com › facebook › create-react-app › issues › 4715
css modules not working in production (heroku) · Issue #4715 · facebook/create-react-app
July 1, 2018 - test: /\.css$/, loader: ExtractTextPlugin.extract( Object.assign( { fallback: { loader: require.resolve('style-loader'), options: { hmr: false, }, }, use: [ { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, localIdentName: '[name]__[local]__[hash:64:5]', // minimize: true, // sourceMap: shouldUseSourceMap, }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, ], }, extractTextPluginOptions ) ), // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
Author   OriAmir
🌐
Reddit
reddit.com › r/reactjs › css modules is not working in functional component help?????
r/reactjs on Reddit: CSS Modules is not working in functional component Help?????
March 19, 2020 -

i am facing issue when i am trying to scope css file to specific component in case of class component its working fine by following this article https://create-react-app.dev/docs/adding-a-css-modules-stylesheet

but in functional component this approach is not working

here is some my application details
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-scripts": "3.4.0"

here is my component
import React, { Component } from 'react';
import LS from './Layout.module.css';
// import './Layout.css'
import Aux from '../../hoc/Aux';

// const layout = (props) => (
//
// Toolbar Sidebar BackDrop
//
// {props.children}
//
//
// );

class layout extends Component {
render() {
return (

Toolbar Sidebar BackDrop

{this.children}

);
}
}
export default layout;

when i apply class approach the css is only scope to the layout file but i want to use finctional component but the scope css is not working when i use functional component

🌐
GitHub
github.com › jestjs › jest › issues › 10024
Problems with create-react-app + typescript + CSS Modules + SASS · Issue #10024 · jestjs/jest
July 26, 2016 - I am using a React app using create-react-app, CSS Modules with SASS, and Typescript. For files that do not need styles, the automated tests work great. However, when I test some file that has styles I get this error: Some relevant files...
🌐
Stack Overflow
stackoverflow.com › questions › 48395860 › css-modules-not-working-even-after-ejecting-create-react-app
reactjs - css modules not working even after ejecting create-react-app - Stack Overflow
August 20, 2024 - { test: /\.css$/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, //added this localIdentName: "[name]__[local]___[hash:base64:5]" //added this }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, ], }