How to fix Module not found and Can not resolve @emotion react?

  • react-js
  • 72 Views
  • 2 Min Read
  • 8 Jul 2024

In this solution, we'll show you how to completely solve this error: "Module not found: error: cannot resolve '@emotion/react' ". So let's get started.

 

Before we begin, have a look at the error that is occurring:

 

 

Problem

 

React Error

 

This error indicates that the package "@emotion/react" is not perfectly installed in your project. It happens when you're using Material UI, but there's a problem with this package in your code. There could be different reasons for this like you installed the packages in a different place than your project's main folder, or you forgot to install them, or there might be issues with the cached files causing this error.

 

No matter what caused the issue, this solution will fix the error completely.

 

 

Solution

 

To fix the error "Module not found: error: cannot resolve '@emotion/react'", you'll need to install the @emotion/react package in your project's root directory. You can do this by using the following commands in your terminal, depending on your package manager:

 

After the installation is complete, you can start the app by running the command npm start.

 

#👇️ Using NPM

npm install @mui/icons-material @mui/material @emotion/styled @emotion/react @emotion/core

#👇️ Using YARN

yarn add @mui/icons-material @mui/material @emotion/styled @emotion/react @emotion/core

 

Make sure to separately install all the packages that you're using but that were not included in the previous command. For instance, if you're using the @emotion/css package, install it using the following command:

 

npm install @emotion/css

 

If you've installed all the necessary packages for your project but the error persists, try closing your code editor and restarting it. Sometimes, this simple step can help resolve the error.

 
import RecommendIcon from '@mui/icons-material/Recommend';
import { jsx, css, Global, ClassNames } from '@emotion/react';

function Navbar() {
  return (
    <div>
      <p css={css`color: teal;`}>
        Hello React
        <RecommendIcon />
      </p>
    </div>
  );
}

export default Navbar;

 

We hope you've successfully resolved the error by now. But, if you're still encountering it, you can try the following additional steps:

 

 

Check the dependencies section in your package.json file

 

Make sure all the necessary packages are listed under the dependencies object section of your package.json file.

 

Package Json file

 

Ensure that you have installed the @emotion/react module. If all installed packages are listed under the dependency object, try resolving the error by stopping and restarting your project.

 

If you've installed these packages but they're not listed in the dependency object, you may need to clear the cache and reinstall the latest versions. This could happen if you've installed an older version. Start by clearing the npm cache, then install the latest versions of the packages separately. Use the following commands to clean the npm cache and to install the latest version of packages, respectively.

 

#👇️ Clean npm cache

npm cache clean --force

 

AND

 

#👇️ Using NPM

npm install @emotion/react@latest

npm install @emotion/core@latest

npm install @emotion/styled@latest

npm install @mui/material@latest

npm install @mui/icons-material

 

OR

 

#👇️ Using YARN

yarn add @emotion/react@latest

yarn add @emotion/core@latest

yarn add @emotion/styled@latest

yarn add @mui/material@latest

yarn add @mui/icons-material

 

With this simple solution, you can easily fix the "Module not found and Can not resolve @emotion react" error.

 

If you need a solution to any other web development difficulty, you can ask them. Just put your problem in the question box below, and you will get the answer soon.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!