material-ui: Uncaught TypeError: styled_default is not a function
Duplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Summary š”
I updated my package.json and sometimes I get this error and itās not going away. But sometimes I donāt get it.
The problem is two-folded:
- Itās intermittent (sometimes appears and sometimes goes away automatically)
- It does not tell us more information (what code of mine has caused it?)
Examples š
No response
Motivation š¦
Please add more info to it. Please let us know what have we done wrong to get this error.
I have tried things proposed on StackOverflow and https://github.com/mui/material-ui/issues/32727 but none worked for me.
Please give us more info. Thatās the feature Iām requesting. Thank you.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 2
- Comments: 24 (3 by maintainers)
Iām seeing this as well, but with the Grid2 component:
Problem description
Iāve been experiencing the
styled_default is not a functionissue in a Yarn workspaces monorepo. Itās because I have two workspaces that both use MUI, one workspace has a Next.js application which imports shared components from another workspace within the repo. Weāre doing this so that other Next.js workspaces can also use the same component library.The problem lies in the way Yarn is installing MUI and Emotion packages. For the Next.js application itās installing MUI and Emotion in the workspaceās own node_modules folder. But for the shared components workspace, itās MUI and Emotion packages are being installed in the root node_modules folder.
When the Next.js application runs, any MUI hooks or utils that are imported from the workspaceās node_modules folder arenāt aware of the ThemeProvider context coming from MUI and Emotion in the root node_modules folder.
Potentially hacky solution:
When I removed MUI and Emotion packages from the individual workspaceās package.json files and moved them up to the root package.json file, the problem went away. This is because now when the Next.js app runs, the ThemeProvider context is the same for all MUI hooks, utils and components.
Itās worth noting that I needed to delete the .next cache in the workspace and re-run the local server to ensure that no cached MUI and Emotion data was being used.
Potential downsides
A downside to this could be that now all the workspaces in the monorepo would have to use the same MUI version. This is probably a requirement in this scenario because if the versions varied then MUI or Emotion would be installed in the workspacesās own node_modules folders which would trigger the same error as before.
Another downside is the potential confusion this may cause to other developers if they are maintaining or upgrading package versions and itās not clear why the packages have been installed in this way.
Another potential solution
An alternative to manually handling the packages in this way is to pnpm instead of yarn. pnmp has a shamefully-hoist config option which forces a package to be hoisted to the root node_modules folder.
Why was Emotion moved to the root package.json too?
When I was debugging this issue, I experimented with manually deleting the @mui folder from the workspaceās own node_modules folder so that when the project is served locally it uses the @mui folder found in the root node_modules. When I left the emotion folder in the workspaceās node_modules folder the
styled_default is not a functionissue was still present.TL;DR
The issue in monorepos could be due to MUI and Emotion packages not being hoisted to the root node_modules folder which could be preventing the ThemeProvider context from being shared between workspaces.
@TurtleSocks +1
Had the issue few hours ago when I started using
Unstable_Grid2. Fixed the issue (for now?) by updating all my dependencies.For what itās worth here the changes:
It didnāt seem necessary but I also change the imports into:
The Vite team found the bug. Itās in the bundling.
Update on mine, Iām still not 100% sure what caused it, but after multiple days of troubleshooting, it has started to work again, so here are some things I tried along the way.
mainto the current branch that I was working onstyled_default is not a function at...issue.main, also workedimport Grid2 from '@mui/material/Unstable_Grid2/Grid2'=>import Grid2 from '@mui/material/Unstable_Grid2', despite both the working and non-working Vite apps having a mixed combination of them previouslyimport { Button } from '@mui/material'=>import Button from '@mui/material/Button'import { ThemeProvider } from '@mui/material'=>import { ThemeProvider } from '@mui/material/styles'and this miraculously seems to have fixed it. Whatās odd is that I had tried this earlier in the week, with no results, but after changing the other imports, it seems to have resolved it?5.10.3, the one that was always working still has imports like this:import { Button } from '@mui/material', including the ThemeProvider component, so itās unclear to me why changing the imports in the offending app has made a difference but at least itās working now.Hope this helps š
Thanks, today I was writing my personal project and inexplicably got
error.
When I checked against your troubleshooting steps and changed the "
@mui/material/Unstable_Grid2/Grid2"dependency to ā@mui/material/Unstable_Grid2ā, my project was able to run normally again. This works great for me.I had the same problem, after I used Unstable_Grid2
change import statement to import ThemeProvider from ā@mui/material/styles/ThemeProviderā;
But they donāt clarify the solution, Iāve been reading about the issue for 3 hours and thereās no solution, Vite is totally buggy