reactjstypescriptwebpackmaterial-uipatch-package

getUtilityClass is not a function. MaterialUI (MUI) error after just adding webpack server


I got this error after starting up my project: Browser Runtime Error. The only thing I did was to add webpack. This is the config file for webpack reference:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const config = {
  entry: './src/index.tsx',
  mode: 'development',
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    noParse: [/\/node_modules\/@tanstack\/react-query-devtools\//],
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.png$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              mimetype: 'image/png'
            }
          }
        ]
      },
      {
        test: /\.svg$/,
        use: 'file-loader'
      },
      {
        test: /\.ts(x)?$/,
        loader: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new Dotenv(),
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
  ],
  resolve: {
    extensions: [
      '.tsx',
      '.ts',
      '.js'
    ]
  },
  devServer: {
    port: 3000, // Add port 3000 to the development server
    hot: true, // Enable Hot Module Replacement (HMR)
    open: true, // Open the application in the default browser when the server starts
    static: {
      directory: path.resolve(__dirname, 'dist'), // Set the base directory for serving files
    }
  }
};

module.exports = config;

And I started getting materialUI error. I thought at first that it was because I was accidentally updating the package json file but that wasn't the case. I also want to point out the fact that it worked before adding webpack. All I did was to configure the webpack and install some dependencies (which did not affect previous dependencies or versions!). I also got one more error after adding webpack to the project which I fixed using 'patch-package' library, with this patch file:

diff --git a/node_modules/@mui/material/SvgIcon/SvgIcon.js b/node_modules/@mui/material/SvgIcon/SvgIcon.js
index 9c80c7b..b254159 100644
--- a/node_modules/@mui/material/SvgIcon/SvgIcon.js
+++ b/node_modules/@mui/material/SvgIcon/SvgIcon.js
@@ -8,7 +8,7 @@ import { unstable_composeClasses as composeClasses } from '@mui/base';
 import capitalize from '../utils/capitalize';
 import useThemeProps from '../styles/useThemeProps';
 import styled from '../styles/styled';
-import { getSvgIconUtilityClass } from './svgIconClasses';
+import svgIconClasses from './svgIconClasses';
 import { jsx as _jsx } from "react/jsx-runtime";
 import { jsxs as _jsxs } from "react/jsx-runtime";
 const useUtilityClasses = ownerState => {
@@ -20,7 +20,7 @@ const useUtilityClasses = ownerState => {
   const slots = {
     root: ['root', color !== 'inherit' && `color${capitalize(color)}`, `fontSize${capitalize(fontSize)}`]
   };
-  return composeClasses(slots, getSvgIconUtilityClass, classes);
+  return composeClasses(slots, svgIconClasses, classes);
 };
 const SvgIconRoot = styled('svg', {
   name: 'MuiSvgIcon',

This was the previous error that was fixed through the patch:

I tried with multiple verions of node(nodejs) to see if I can encounter the error multiple times. And it doesn't work in both node 14.15.0 nor 18.15.0.

Another strange thing is that I can't even find "getUtilityClass" word anywhere in my project, using ctrl + shift + f in vsCode to find it.


Solution

  • The problem was indeed with the dependencies. I probably missed something when installing some libraries. When I reverted to the old package-json and added only the dependencies I needed, after npm install the project worked!