Search code examples
csswebpacknode-modulescss-loader

Css of node-modules


I am using css modules in my react application. After setting modules:true inwebpack.config.js, I am able to get css from files which I created in my src folder. I am importing some components from node-modules. But the css of components present in node-modules is not getting applied.

I also tried importing css of node-modules using import 'path'; but it didnt work.

If I set modules:false, css of node-modules is getting applied and css of files in src folder is not getting applied.

webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: "style-loader"
          },
          {
            loader: "css-loader",
            options: {
              modules: true,
            },
          }
        ],
      },
    ],
  },
};

Solution

  • This might be due to the node module you are using may not be following module syntax. I mean they might be setting classes directly with a string instead of using like style.someClassName. So, you need 2 entries in your rules 1 for your stuff and 1 for node modules.

    for ex:

    module.exports = {
        module: {
            rules: [{
                test: /\.css$/,
                exclude: /node_modules/,
                use: [{
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                        },
                    }
                ],
            }, {
                test: /\.css$/,
                include: /node_modules/,
                use: [{
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            modules: false,
                        },
                    }
                ],
            }],
        },
      };