Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

MUI v5 crashes with Cannot read properties of undefined (reading 'create')

ErrorTypeError: Cannot read properties of undefined (reading 'create')

Short answer

In MUI v5 the old makeStyles and withStyles from @mui/styles no longer get a default theme. Without a ThemeProvider at the root the theme is missing, so code that reads a theme value and calls .create on it fails. Add a ThemeProvider from @mui/material/styles, or move the styles to sx or styled.

  • Applies to MUI 5, React 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the stack trace for your own file, then check whether that file or a parent uses makeStyles, withStyles or a theme value. The trace points at the style function that reads the theme.

    grep -rn "@mui/styles\|makeStyles\|withStyles" src
  2. 2Match what you see, then jump to its fix

    1. 1The crash happens in a component that uses makeStyles or withStylesmakeStyles or withStyles runs with no ThemeProvider at the rootFix
    2. 2A ThemeProvider exists but the import line says @mui/styles, and Material UI components inside it still crashThemeProvider is imported from @mui/styles, not from @mui/material/stylesFix
    3. 3Many files import from @mui/styles and new components keep hitting theme errorsThe legacy JSS styling is still in use and should be migratedFix

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'create')

Cause 1 of 3

makeStyles or withStyles runs with no ThemeProvider at the root

1Check

The MUI v5 migration guide states that makeStyles is no longer exported by @mui/material/styles, and tells you to add a ThemeProvider at the root because the defaultTheme is no longer available.

grep -rn "ThemeProvider" src

2Fix

Create a theme and wrap the whole app in ThemeProvider from @mui/material/styles.

import { createTheme, ThemeProvider } from "@mui/material/styles";

const theme = createTheme();

root.render(
  <ThemeProvider theme={theme}>
    <App />
  </ThemeProvider>
);

3Verify

Reload. The component renders and the TypeError is gone.

npm run dev

Confirmed by mui.com

Did this fix it?

Cause 2 of 3

ThemeProvider is imported from @mui/styles, not from @mui/material/styles

1Check

The migration guide says to replace the use of ThemeProvider from @mui/styles with the one from @mui/material/styles, which makes the theme available to both the @mui/styles utilities and Material UI components.

grep -rn "ThemeProvider" src | grep "@mui/styles"

2Fix

Change the import. makeStyles itself can stay for now if you import it from @mui/styles/makeStyles.

// before
import { ThemeProvider } from "@mui/styles";

// after
import { ThemeProvider } from "@mui/material/styles";

3Verify

Run the app and open the page that failed.

npm run dev

Confirmed by mui.com

Did this fix it?

Cause 3 of 3

The legacy JSS styling is still in use and should be migrated

1Check

The JSS migration guide allows JSS overrides for a while after the upgrade, and recommends moving to sx or styled. It also says @emotion/styled must stay installed, as it is a peer dependency of @mui/material.

npm ls @emotion/react @emotion/styled @mui/styles

2Fix

Move each makeStyles block to the sx prop or styled(), then remove @mui/styles when nothing imports it.

// after: styles move into the sx prop
<Chip sx={{ py: 1, px: 1.5, boxShadow: 1 }} label="New" />

# then, when no file imports @mui/styles
npm uninstall @mui/styles

3Verify

Search the source; no file imports @mui/styles.

grep -rn "@mui/styles" src

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • Wrap the app in one ThemeProvider from @mui/material/styles, even when you use the default theme.
  • Write new styles with sx or styled, and migrate old makeStyles code file by file.
  • Keep @emotion/react and @emotion/styled in package.json.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.