Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Can't resolve '@emotion/react' in 'E:\frontend\node_modules\@mui\styled-engine'

I am trying to import a box component from @mui/material/Box. I have installed MUI 5 using the following command npm i @mui/material. But this error is occurring, Module not found: Can't resolve '@emotion/react' in 'E:\frontend\node_modules\@mui\styled-engine'

Here is my code.

import * as React from 'react';
import Box from '@mui/material/Box';

export default function WhiteSpace() {
  return (
    <div style={{ width: 200 }}>
      <Box
        component="div"
        sx={{
          whiteSpace: 'nowrap',
          my: 2,
          bgcolor: 'background.paper',
        }}
      >
        White Space Nowrap. White Space Nowrap.
      </Box>
      <Box
        component="div"
        sx={{
          whiteSpace: 'normal',
          my: 2,
          bgcolor: 'background.paper',
        }}
      >
        White Space Normal. White Space Normal.
      </Box>
    </div>
  );
}

Can someone help me fix this?

like image 302
stmp_lee Avatar asked Sep 21 '21 13:09

stmp_lee


People also ask

How do I install emotions in react?

There are lots of ways to use Emotion, if you're using React, the easiest way to get started is to use the @emotion/react package. If you're not using React, you should use the @emotion/css package. To use it, import what you need, for example use the css prop to create class names with styles.

Why are emotions over styled components?

The biggest advantage of Emotion is its easily handled object styles for writing CSS. Take, for example, the case of styled-components, wherein the developer must create unique names for different components, all while avoiding identical naming styles.


Video Answer


2 Answers

@emotion/react and @emotion/styled are peer dependencies of many mui packages, ie @mui/material. This gives us more freedom over which emotion version we would like to use, but does mean we must install the dependencies ourselves.

npm i @emotion/react @emotion/styled
like image 109
Peter Hayman Avatar answered Oct 04 '22 15:10

Peter Hayman


One of my friend faced the same issue couple of days ago, the solution is as follows:

Step 1: npm i @emotion/react Step 2: It will give same error with different dependency name, install it with npm and your app will run.

like image 31
Muhammad Farhan Avatar answered Oct 04 '22 17:10

Muhammad Farhan