1

I am learning react looking videos but in one video they are enabling css module by eject>edit webpack.config.dev.js but i can't find the same file in my react , later i came to know that in 16.7 its different so can anyone tell the steps to enable css module in react 16.7

2
  • It's a webpack configuration, so it shouldn't matter which react version you have. After ejecting, search die sub directories of your project for files named like webpack*.js Commented Feb 11, 2019 at 10:01
  • i found one but in that also there is different structure , so where to add Modules:true, LocalIdentName: etc Commented Feb 11, 2019 at 10:03

3 Answers 3

4

fortunately in react 16.8 no need to run "npm run eject" and you can just add extention ".module.css" in place of ".css" to get the sake of CSS module

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

Comments

2

To enable CSS module, first of all go to your project directory then open command line and run npm run eject

inside the config folder you will find webpack.config.dev.js and webpack.config.prod.js files.open those files

and add this code to webpack.config.dev.js

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

and in filewebpack.config.prod.js add

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

After saving this you can now use CSS module

1 Comment

ok i found first one but not the second one i.e config.prod.js , but it helped me thanks
0
For react version 16.13
In webpack.config.js file find this keyword 'css-loader'.
You will find below code in line number 82 for react version-16.13
{
   loader: require.resolve('css-loader'),
   options: cssOptions,      
}

Replace above object with 

{
   loader: require.resolve('css-loader'),
   options: {
      modules: {
        mode: "local",
        localIdentName: "[name]_[local]_[hash:base64:5]"
      },
      import: true,
      importLoaders: true
    }     
}

Start the server again by npm start(If changes are not reflected)

Comments

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.