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 OverflowI 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.
in react 16.13 and [email protected] and higher you don't need to eject your project.
Your CSS files must be named camelCase + .modules.css and import to your projects like this:
import React, { Component } from 'react';
import styles from './app.module.css'; // Import css modules stylesheet as styles
class App extends Component {
render() {
return <button className={styles.test}>Error Button</button>;
}
}
export default App;
and in app.module.css
.test{
color: blue;
}
if you eject projects, in webpack.config.js file, in css modules change like this:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
modules: true,
sass CSS Module Not Working
reactjs - Module.css is not working in React.So css can't proceed either. Please reply - Stack Overflow
"Cannot resolve module" for CSS files
In v5 can't use import * as styles with css modules, bug?
Videos
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]
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
TypeScript does not know that there are files other than .ts or .tsx so it will throw an error if an import has an unknown file suffix.
If you have a webpack config that allows you to import other types of files, you have to tell the TypeScript compiler that these files exist. To do so add a declaration file in which you declare modules with fitting names.
The content of the module to declare depends on the webpack loader used for the file type. In a webpack configuration that pipes *.scss files through sass-loader → css-loader → style-loader, there will be no content in the imported module, and the correct module declaration would look like this:
// declaration.d.ts
declare module '*.scss';
If the loaders are configured for css-modules just extend the declaration like this:
// declaration.d.ts
declare module '*.scss' {
const content: Record<string, string>;
export default content;
}
Just add the file typings.d.ts containing:
declare module "*.module.css";
and remember to declare your css files with 'module'. for example styles.module.css
importing:
import React from 'react';
import styles from './styles.module.css';
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
i'm using material-ui and I kind of faced the same problem, I had Header and Footer component inside of layout.js and none of my styles was applying till I used !important which is really annoying. so what I did was injecting my css to material ui on the top of my component trees like this :
import { StyledEngineProvider } from '@mui/material/styles'
export default function RootLayout({ children }) {
return (
<html lang="fa-IR" dir='rtl' className={vazir.className}>
<body>
<StyledEngineProvider injectFirst> //this part is important
<MainTheme>
<MainContext>
<DesktopHeader />
<MobileHeader />
<Box sx={{ backgroundColor: '#101820' }}>
{children}
<Footer />
</Box>
</MainContext>
</MainTheme>
</StyledEngineProvider>
</body>
</html >
)
}
Just to add my own point as I had a 'not working .module.css issue too'
In my case, my css class names was using hyphen (-)
into my component_foo.module.css:
/* NOT WORKING */
.inclined-bg-type1 {
background-color: orange;
}
was converted into inclinedBgType1 in my component_foo.tsx but was not applied (after import styles from './component_foo.module.css')
<div className={`h-96 w-full ${styles.inclinedBgType1}`}></div>
CORRECTED BY USING _ instead of -:
.inclined_bg_type1 {
background-color: orange;
}
leads to inclined_bg_type1
<div className={`h-96 w-full ${styles.inclined_bg_type1}`}></div>
and works just as expected.
It is a known behavior (https://github.com/facebook/create-react-app/issues/11155) but I still spend an hour trying to correct it.
If it may help others...
The loaders are applied in reverse order. That means you're applying style-loader first and then pass its result to css-loader. The output of style-loader is JavaScript, which will insert the styles into a <style> tag, but css-loader expects CSS and fails to parse JavaScript as it is not valid CSS.
The correct rule is:
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}
I was having the same problem and eventually found that there was a second module rule in the webpack config that was processing *.css files
I just upgraded to Next 13 and switched to Typescript, so now I'm rewriting my web site. But VS Code cannot seem to find the module.css files. Files like global.css import just fine. Here's an example:
import './global.css'; import styles from './layout.module.css';
global.css can be found by VS Code, but layout.module.css cannot. However, when the code runs, both of these files import just fine. Any clue as to what I'm doing wrong?
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?
EDIT -> 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?
EDIT 2 -> 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.
EDIT 3 -> 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.
Here is a github repo if it helps: https://github.com/randobanohando/testprj