12

I have created a project using vite and react. I create a theme to set my project into right to left. Everything was ok and the project was running properly.

const theme = createTheme({
  direction: 'rtl' , 
  typography: {
    "fontFamily": `"iransans"`,
    "fontSize": 11,
    "fontWeightLight": 300,
    "fontWeightRegular": 400,
    "fontWeightMedium": 500
   }
})
const cacheRtl = createCache({
  key: "muirtl",
  stylisPlugins: [prefixer, rtlPlugin]
})


function App() {
  let history = useHistory();
  let contained = "Test The";
  return (
    <div className="App">
      <Router>
        <div>
          <Switch>
            <CacheProvider value={cacheRtl}>
              <ThemeProvider theme={theme}>
                <Route exact path="/applicant">
                  <Applicant />
                </Route>
              </ThemeProvider>
            </CacheProvider>
          </Switch>
        </div>
      </Router>
    </div>
  )
}
export default App

After I add a Slide component to my project. Suddenly my project stop working and the console is showing the

Box.js:5 Uncaught TypeError: createTheme_default is not a function at Box.js:5:22

I rollback my changes(by git checking). but the error is still showing.

I can't understand what's going on Why is the error still there after reverting the changes?

Mehdi
  • 520
  • 1
  • 5
  • 9

19 Answers19

13

Not using ThemeProvider just plain MUI. I found that the error was coming from my app.tsx page where I import CSSBaseline.

Moving CSSBaseline above Box import on first page solve the issue:

From:

import Box from "@mui/material/Box";
import CssBaseline from "@mui/material/CssBaseline";

To:

import CssBaseline from "@mui/material/CssBaseline";
import Box from "@mui/material/Box";
user1028468
  • 141
  • 1
  • 3
  • Wow! Of all the solutions, this worked for me! – anjandash Apr 20 '23 at 17:56
  • Put `import Box from '@mui/material/Box';` after other mui components worked for me (e.g. after `import Button from '@mui/material/Button';`) – fl_ May 27 '23 at 15:40
11

I've discovered there is a problem with the Vite cache, try running your development command with the --force flag to fix the cache issues:

yarn dev --force

or with npm

npm run dev --force

Warwick
  • 181
  • 9
6

For me it was because of imports of MUI Box component:

import Box from '@mui/material/Box';
import { BoxProps } from '@mui/material/Box/Box';
import { useTheme } from '@mui/material/styles';

I changed it to

import { Box } from '@mui/material';
import { BoxProps } from '@mui/material';
import { useTheme } from '@mui/material';

And it worked

Tony I.
  • 498
  • 4
  • 16
4

I had the same error so I removed all material ui dependency and reinstalled them, it worked for me.

yarn remove @mui/material @emotion/react @emotion/styled 

or

npm uninstall @mui/material @emotion/react @emotion/styled 
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Lahiru
  • 47
  • 4
4

I had the same issue and I fixed it by removing the node_modules/.vite/deps folder and restarting the server.

Dinuka Dilshan
  • 552
  • 6
  • 12
3

The main error is due to not getting the availability of the theme provider before accessing the components, or, in other cases, a vite-cache issue.

To solve the first case, wrap you App component inside theme provider.

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

const theme = createTheme({
   direction: 'rtl',
   // other theme properties
});

function App() {
   return (
      <ThemeProvider theme={theme}>
          <div className="App">
             ...
          </div>
      </ThemeProvider>
   )
};

Now in case of second problem, restart your server with --force flag.

For ex: npm run dev --force

1

I had been getting the same error in my project... I tried npm run dev --force to delete MUI version cached in Vite (as suggested)... I checked all imports (import Box from ...) to be (import { Box } from...)...

But then, I realized that I might have placed wrongfully the Engine provider and Theme Provider. In my case, I was using suspense, and the Wrapper tags of the providers were wrapping the Component outside the Suspense tag. That was enabling a function before it already exists, hence causing that error.

I moved the Providers inside the Suspense, and it worked as expected.

Kvz
  • 1,141
  • 1
  • 3
  • 9
1

I fixed it by simply downgrading the mui. This version works in my case.

"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
"@mui/icons-material": "^5.10.6",
"@mui/material": "^5.10.7"
1

I had the same issue.

The fix for me was to make sure ThemeProvider was imported before Box. Hope this helps anyone in a similar situation.

Josh
  • 535
  • 6
  • 14
1

Your components should have a correact import path:

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

It's doesn't work

import { ThemeProvider, createTheme, styled } from '@mui/material';
Liam
  • 27,717
  • 28
  • 128
  • 190
1

As other people say, fixing the import in App.tsx fixed the issue, at least for me.

Before (Here I get ThemeProvider and createTheme from @mui/material):

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

After (Correctly imported from @mui/material/styles):

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

You can see how the people from Material UI import this element in their Troubleshooting page.

Another thing, the order of imports also matters, so ThemeProvider and createTheme should be before any other element that uses them.

Hope it helps!

0

I think this is given inside the index.js not in App.js We give Routes in App.js only not the Providers

Shilpe Saxena
  • 219
  • 2
  • 8
0

I fixed it by changing the imports

import {ThemeProvider} from '@emotion/react'-

import {ThemeProvider} from '@mui/material'+

lanworm
  • 1
  • 1
0

Changing the theme provider import in my App.jsx file fixed it for me.

Previously: import { StyledEngineProvider } from "@mui/material";

Now: import { StyledEngineProvider } from "@mui/material/styles";

0

Remove node_modules and install again, and restart your app

0

Had the same bug. Cause:

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

Solution:

import createTheme from "@mui/material/styles/createTheme";
Amos
  • 91
  • 3
0

I Fixed it by removing the node_modules folder my the project and then reinstalled it by using npm install

Gaurav
  • 1
0

delete .vite (build cache folder inside node_modules) and restart the dev server

Alexander
  • 79
  • 5
-4

I solved it doing the following steps:

  1. open node_modules/@mui/material/Box/Box.js
  2. remove line 5: const defaultTheme = createTheme();
  3. remove line 7: defaultTheme,
  4. restart yarn dev --force
Asplund
  • 2,254
  • 1
  • 8
  • 19