I had this same problem and solved it by renaming my css file to:

myName.module.css

and also importing like this:

import styles from './myName.module.css'

There is no need to follow the steps of updating the webpack files any more.

Answer from pflevy on Stack Overflow
🌐
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
November 2, 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]

Discussions

reactjs - How to use css modules with create-react-app? - Stack Overflow
According to a tweet by Dan Abramov, CSS modules support is there in create-react-app (CRA). One just needs to give extension of module.css to his stylesheets to enable the feature, but this is not More on stackoverflow.com
🌐 stackoverflow.com
Newest 'react-css-modules' Questions - Stack Overflow
I’m working on a React app for my college project (using React + Vite) and I recently tried changing my theme colors defined in :root. However, the new values are not reflected across my site — only ... ... I'm currently developing with React Vite Tailwind "tailwindcss": "^4.1.4" and css modules... More on stackoverflow.com
🌐 stackoverflow.com
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
December 18, 2021
CSS Modules in React JS not working, i have tried literally everything but it didn't solved
Everything seems correct but I don't know why I can't able to use CSS in my project. I have tried without writing module in CSS file's name but it didn't work. I have tried literally everything but... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 69645584 › react-css-modules-is-not-working-correctly
react-css-modules is not working correctly
November 27, 2021 - "react-dom": "^17.0.2", "react-redux": "^7.2.5", "redux": "^4.1.1", "redux-thunk": "^2.3.0", "style-loader": "^3.3.0", "webpack": "^5.58.2", "webpack-cli": "^4.9.1", "webpack-dev-server": "^4.3.1" } } ... import React from "react"; import styles from "./myBtn.module.css"; const Component=()=>{ return( <div> <button className={styles.myBtn}>1234</button> </div> ) } export default Component;
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.

🌐
Stack Overflow
stackoverflow.com › questions › tagged › react-css-modules
Newest 'react-css-modules' Questions - Stack Overflow
However, at seemingly random times, the CSS stops loading (as far as I can observe from ... ... I'm trying to setup Rspack inside a Nx monorepo that uses React, module federation, Typescript support and css modules using scss. I got almost everything done except for the last part.
🌐
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
December 18, 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
🌐
Stack Overflow
stackoverflow.com › questions › 68071522 › css-modules-in-react-js-not-working-i-have-tried-literally-everything-but-it-di › 68374253
CSS Modules in React JS not working, i have tried literally everything but it didn't solved
react-css-modules · Share · Improve this question · Follow · asked Jun 21, 2021 at 16:26 · Faraaz Mohammed · 1111 silver badge22 bronze badges 1 · please try with normal CSS, import './App.module.css'; className="blue" Supriya Kumari – Supriya Kumari · 2021-06-21 17:33:55 +00:00 Commented Jun 21, 2021 at 17:33 ·
🌐
Reddit
reddit.com › r/learnjavascript › trying to add css modules to my react project in my js file
r/learnjavascript on Reddit: Trying to add CSS Modules to my react project in my js file
June 20, 2024 -

I'm having a problem when using CSS Modules with React + Webpack where my variable styles is undefined and nothing can be found. I'm using the following in my project:

"devDependencies": {
    "@babel/core": "^7.24.7",
    "@babel/preset-env": "^7.24.7",
    "@babel/preset-react": "^7.24.7",
    "babel-loader": "^9.1.3",
    "css-loader": "^7.1.2",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.6.0",
    "style-loader": "^4.0.0",
    "webpack": "^5.91.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^5.0.4"
  }

My webpack.config.js:

const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.js',

    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'bundle.js'
    },

    plugins: [
        new HTMLWebpackPlugin({
            template: './src/index.html'
        })
    ],

    module: {
        rules: [
            {
                test: /.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-env',
                            '@babel/preset-react'
                        ]
                    }
                }
            },
            {
                test: /\.(png)$/i,
                use: [
                    {
                        loader: 'file-loader',
                    }
                ]
            },
            {
                test: /\.css$/,
                use: ['style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1,
                            modules: true
                        }
                    }
                ]
            }
        ]
    }
}

I have a file called TestComponent.js which has:

import React from 'react';
import styles from './Test.module.css';

function TestComponent() {
    console.log(styles);

    return (
        <div className={styles.a}>
            <p>
                Random text Random text Random text Random text Random text Random text
            </p>
        </div>
    );
}

export default TestComponent;

My css file is called Test.module.css:

.a {
    border-style: solid;
    border-color: green;
    border-width: 2px;
}

Not sure what is going wrong? I've tried a few things so far...

Using: "import * as styles" seems to work but every guide I see online shows the way I did it above, so I'm confused as to why it wont work?

I found that using "import { a } from './Test.module.css';" works, but I still cannot get it to work if I try to do it the above way.

If I remain using styles but don't actually use it in a className it does get added into the index.html header as a style tag. I'm just unable to use it in the component.

I've also adjusted webpack.config.js a bit without any luck

{
    test: /\.css$/, // Apply CSS Modules only to .module.css files
    exclude: /\.module\.css$/,
    use: ['style-loader', 'css-loader']
    },
    {
    test: /\.module\.css$/, // Apply CSS Modules only to .module.css files
    use: [
        'style-loader', // Inject styles into the DOM
        {
        loader: 'css-loader', // Translates CSS into CommonJS
        options: {
            modules: {
            localIdentName: '[name]__[local]__[hash:base64:5]', // Custom naming pattern for CSS Modules
            },
        },
        },
    ],
    }

Github repo: https://github.com/randobanohando/testprj

Find elsewhere
🌐
OpenReplay
blog.openreplay.com › using-css-modules-in-react
Using CSS Modules in React
September 2, 2022 - You only need to change the CSS file name to “[filename].Modules.css”; you can substitute any other name for “[filename]”. You must use the import keyword when using CSS Modules to import the file to a specific component. You must specify the classes when integrating CSS Modules into your React projects, like how you would access an object’s property in standard Javascript using the dot notation or the bracket syntax.
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 › vercel › next.js › issues › 36873
module.css not loading styles · Issue #36873 · vercel/next.js
May 12, 2022 - The only place on the whole project that a css references .live-stream is on App.module.css, I also tried chaging the main element to a div with class 'main' and the changes were not picked up anymore. Its like main tag works but not class names.
Published   May 12, 2022
Author   andres-dejesus
🌐
Pusher
pusher.com › blog › css-modules-react
Getting started with CSS modules in React | Pusher blog
We have considered how you can use CssModules to change the way you work with styles in your React application. You can combine this method with a CSS preprocessor like Sass or Less. We also learned about the methods of including CSS in our React components. While both methods share certain advantages, the implementation differs: one is actual CSS living in CSS files wired with JS modules using imports and hashed class names at build time, the other is actual JS from the start and is composed and attached to elements in the form of inline styles at execution time.
🌐
Envato Tuts+
code.tutsplus.com › home › javascript
Learn React 18: Using CSS Modules | Envato Tuts+ - Code
August 4, 2023 - CSS modules make sure that your CSS is locally scoped by adding a unique hash to your class names. You will understand it better once we use CSS modules for styling our random number generator...
🌐
GitHub
github.com › vercel › next.js › issues › 33286
CSS Modules sometimes don't load when using next/dynamic import · Issue #33286 · vercel/next.js
January 13, 2022 - Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101 Binaries: Node: 14.17.0 npm: 6.14.13 Yarn: 1.22.10 pnpm: N/A Relevant packages: next: 12.0.8 react: 17.0.2 react-dom: 17.0.2 ... When loading a component using next/dynamic, CSS modules are sometimes not loaded correctly. This seems like it may be similar to #18252, which is described as fixed (although there is no linked PR showing the fix). This bug only occurs when running next build + next start. Works as expected in development (next dev).
Author   jzxhuang
🌐
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

🌐
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
🌐
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
🌐
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. This way create-react-app knows that the file should be treated as CSS Module. After this you can import the CSS file normally and use the class names from it to style your components.