vite: Popper.js:11 Uncaught TypeError: styled_default is not a function at Popper.js:11:20
Describe the bug
I have updated some of my dependencies.
I expect to see the address of my code in the error call stack.
All I see is this:
Popper.js:11 Uncaught TypeError: styled_default is not a function
at Popper.js:11:20
(anonymous) @ Popper.js:11
I have also reported it to the MUI Team
I have also seen that it’s been asked here.
My problem is that the error does not give us more information. Is it a Vite bug? Is it a bug related to esbuild? Is it a bug of MUI? I literally do not know.
Reproduction
https://github.com/Nefcanto/MuiPopperBug
Steps to reproduce
git clone https://github.com/Nefcanto/MuiPopperBug
cd MuiPopperBug
npm install
npm run dev
- Browse and see the error
System Info
System:
OS: Linux 5.19 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
Memory: 9.10 GB / 15.52 GB
Container: Yes
Shell: 5.1.4 - /bin/bash
Binaries:
Node: 18.15.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 9.6.1 - /usr/local/bin/npm
npmPackages:
@vitejs/plugin-react: ^3.1.0 => 3.1.0
vite: ^4.1.4 => 4.1.4
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 23
- Comments: 52 (2 by maintainers)
This worked for me as well.
Basically if you have issues with Popper.js
add to vite.config.js
If it’s not Popper.js find the relevant component and include it as an optimized dep.
Hey all, I’ve been trying to fix what I believe is a very similar issue, except for me it affects the
@mui/x-date-pickers
.I’m having the error
Uncaught TypeError: useEnhancedEffect_default is not a function
only when building the app What leads me to believe the issue is related is because of this issue in the mui repo: https://github.com/mui/material-ui/issues/31835 which seems similar to what is described hereWhile working on a minimal reproduction, it seems like the bug exists in version
4.0.0
of vite, but is resolved in4.2.0
Please find an attached repository example: https://github.com/jbsouvestre/vite-esbuild-bug-repro There’s 2 branches, master is on vite@4.2.0, the production build doesn’t have the error
The vite-4.0.0 branch has the bug
If the issue is not related, please disregard this comment
I’m having the same issue but with Grid2
Update: I managed to have the error moved onto Box.js as mentioned in the comment above when I rearranged import order.
I was finally successful in removing the error altogether after further import order rearrangements. I essentially started at my top-level app file and moved a few (not all) material component imports to the top. It seemed to like it when custom components that also use MUI components were imported after MUI components used in the immediate file.
Luckily, I didn’t have to continue that process into other files as my fix was in a top level file.
oh, this is saved my day!
@Tanimodori Thanks for the excellent investigation work.
I’ve narrowed down the issue a bit more and created an esbuild issue: https://github.com/evanw/esbuild/issues/3357.
We encounter the same problem on our project. And we are not able to reproduce systematically the error. We already try to upgrade or downgrade MUI, we check every import. Everything seems right, but sometimes, the appears, sometimes it works perfectly.
So this comment is true on the aspects of this issue
node_modules
and triggers vite to re-generate deps. Vite seems to be more likely to generate big dep chunks after the first generation. So when people installing/uninstalling deps, this issue tends to be “fixed”.I’ve tried all of the recipes without any success. And I think I’ve found a stable workaround. Just add
as a first statement in your
index.tsx
file (or other file). It should be the first import.It propely initalizes
styled
function, so it can be used later.Remove cache folder
node_modules/.vite
and restart your application.If you have a complicated multi-module application and it’s unclear to wich module you should add this import, then you can try the following algorithm:
import '@mui/material/styles/styled';
Thanks. I followed the recommendation above of adding this to vite.config.js
optimizeDeps: { include: [‘@mui/material/Tooltip’], };
and it seems to have done the trick - not sure if this is permanently fixed or just by chance.
Using Vite v4.3.3 MUI v5.12 @mui/x-data-grid-pro v6
Also having same issue in Grid2.js
Seems to happen after a few restart cycles, usually clearing node_modules and npm install makes it go away for a while but not a great solution.
Was there some logic to the re-ordering of imports that seems to work? Moving Mui imports higher doesnt seem to have any effect for me
Same problem here, I cant use popper, getting the error:
I’m using vite, npm and styled-components.
Same Issue with Grid2
But caused by upgrading “@mui/x-data-grid” to 6.1.0. The bug doesn’t happen on 5.x versions;
id2.js:5 Uncaught TypeError: styled_default is not a function at Grid2.js:5:26 (
Update: I removed @mui/x-data-grid, and the app run; When I put it back, without any reference - only in package.json/yarn.lock, the bug came back. So, isn’t seems to be the order of imports.
A fresh clone of this reproduction do reproduce this error, but if I remove the
node_modules
sometimes the problem will be fixed.I see that the cause of this issue and #8308 is missing module initialization.
I replace
src/main.jsx
with the following contentThe
Proper
here is mui-material/src/Popper/Popper.tsxFull transformed code by vite:
The
styled
here is mui-material/src/styles/styled.jsFull transformed code by vite:
Note that in the
styled
module,styled
need to be initialized bycreateStyled
:This suggest we should call
init_styled
prior to accessstyled_default
. But such an assertion is failing inPopper
:Missing calling of
init_styled
fromchunk-QGFUN55Y.js
(the chunk ofstyled
)Almost a year and the issue is still there…Can’t work.
So my process was far from ideal but I just started at my root file and began rearranging imports … i.e. take whatever is a top and move down a level, save, see if anything changed. I knew this didn’t account for cached builds or any potential needs to reinstall node_modules but I just wanted to see if I could get any other errors that might help outline the problem.
I was lucky that it didn’t take long to solve my issue. I didn’t dig further but as mentioned what I noticed is that it seemed to like when I imported pure mui components before other custom built components that also used pure mui components.
Some of the other temporary solutions above also seem like good solutions to try.
I can reproduce the issue at 100%, in my case (in my project, not sure its the case for all i’m gonna try with a codepen)
import { alpha, createTheme, darken, ThemeProvider as MUIThemeProvider, useTheme as useMUITheme, } from "@mui/material/styles";
instead of
import { alpha, createTheme, darken, ThemeProvider as MUIThemeProvider, useTheme as useMUITheme, } from "@mui/material";
When bug occurs I’m fixing the import, removing node_modules folder and npm install again and then bug disapears.
Not @mtgeddes but my 2 cents
I tried a lot of things such as updating most of my dependencies, changing all MUI imports to be from only
@mui/material
e.g. changing imports from@mui/system
, and adding a alias resolution from@material-ui/icons
to@material-ui/icons/esm
.At the end I added an eslint plugin to sort my imports based on it’s own logic and this fixed my issues. I never investigated why, but maybe it had to do with local project modules being imported before MUI in some key files.
I upgraded to notistack v3.x I started to get the same error:
When fall back to notistack 2.0.8 it goes away.
vite 4.3.3 vitejs/plugin-react-swc 3.3.0 mui 5.12.1 mui/x-data-grid-premium 6.2.1
I had the
popper.js
error but it has disappeared after upgrading Vite from 4.1.1 to 4.3.1.edit: it’s back. This issue is popping up daily for at least someone on my team.
edit again: I got rid of
<Popper>
in our codebase and replaced it with<Modal>
.@jbsouvestre The vite version I used for my analysis is 4.2.0 fyi.