0

I want to use React CSS modules with Bootstrap.

First, I created a project with the create-react-app command. Then, I installed Bootstrap 4 with NPM and included it in my app.js like:

import 'bootstrap/dist/css/bootstrap.css';

I also did npm eject to have access to my Webpack config file.

Meanwhile I wanted to use CSS modules in my layout.js file, so I went to webpack.config.dev.js and changed modules to true:

test: /\.css$/,
        use: [
          require.resolve('style-loader'),
          {
            loader: require.resolve('css-loader'),
            options: {
              modules: true       

            }

When I change modules to true Bootstrap CSS is not showing correctly. If I change modules to false then Bootstrap is working. Any idea?

Modules: true

Modules: false

1 Answer 1

0

Here's a good discussion how to use CSS-modules with other global CSS (e.g. Bootstrap)

https://github.com/css-modules/css-modules/pull/65

Hope it helps.

I had same issue and just loaded bootstrap directly in the index.html from public folder. My project not so big to include bootstrap in the same bundle

P.S.

Actually, I also tried this solution and it works good for me. https://github.com/css-modules/css-modules/issues/113

I just renamed all of my global css files as .global..css

{
  test: /^((?!\.global).)*\.css$/,  // anything with .global will not 
go through css modules loader
  loaders: [
    'style-loader',
    'css-loader?modules&sourceMap&importLoaders=1&localIdentName=
[name]__[local]___[hash:base64:5]',
    'postcss-loader'
  ]
}
Sign up to request clarification or add additional context in comments.

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.