Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Material UI build fails because Emotion is missing behind the styled engine

Your error

Module not found: Can't resolve '@emotion/react' in 'E:\frontend\node_modules\@mui\styled-engine'1 more line it can show

Fix it fast

Most common cause, 1 of 2

Emotion was never installed

Install both Emotion packages beside Material UI, the same way the official install command does, and commit the updated lockfile.

Fix
npm install @mui/material @emotion/react @emotion/styled
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Module not found: Can't resolve '@emotion/react' in node_modules/@mui/styled-engine means Material UI's default styling engine imports Emotion and Emotion is not installed. @mui/material marks it as an optional peer, which npm never installs on its own. Run npm install @emotion/react @emotion/styled, or alias @mui/styled-engine to @mui/styled-engine-sc when you use styled-components. Applies to MUI 9.5.

  • Applies to @mui/material 9.5, @emotion/react 11.14, npm 11 and 12, webpack 5.113
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List the styling packages in the tree. Empty entries for both Emotion packages and no styled-components point to cause 1. styled-components or @mui/styled-engine-sc in the list point to cause 2.

Run this first
npm ls @emotion/react @emotion/styled @mui/styled-engine @mui/styled-engine-sc styled-components

Then find the line you see in this list and open its fix:

  1. 1Emotion was never installednpm ls prints (empty) for @emotion/react and @emotion/styled, and the project has no styled-components either
  2. 2The app uses styled-components, but the engine was never swappedpackage.json lists styled-components and @mui/styled-engine-sc, yet the error still names @emotion/react inside @mui/styled-engine

What does the error look like?

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

Cause 1 of 2

Emotion was never installed

You seenpm ls prints (empty) for @emotion/react and @emotion/styled, and the project has no styled-components either

Check

Can't resolve '@emotion/react' in @mui/styled-engine appears when Material UI went in on its own. Emotion is its default styling engine, and @mui/material lists @emotion/react and @emotion/styled as peer dependencies flagged optional in peerDependenciesMeta. npm has installed ordinary peers by default since v7, but its docs say it never installs optional ones, so npm install @mui/material alone leaves Emotion out.

Check
npm view @mui/material peerDependenciesMeta
npm ls @emotion/react @emotion/styled

Fix

Install both Emotion packages beside Material UI, the same way the official install command does, and commit the updated lockfile.

Fix
npm install @mui/material @emotion/react @emotion/styled

Verify

npm ls now shows both packages, and the dev server or production build compiles.

Verify
npm ls @emotion/react @emotion/styled
npm run build

Confirmed by mui.com

Did this fix it?

Cause 2 of 2

The app uses styled-components, but the engine was never swapped

You seepackage.json lists styled-components and @mui/styled-engine-sc, yet the error still names @emotion/react inside @mui/styled-engine

Check

Out of the box @mui/material depends on @mui/styled-engine, which is the Emotion adapter. Adding @mui/styled-engine-sc to the project changes nothing until the bundler loads it wherever @mui/styled-engine is imported, so the Emotion adapter keeps running and keeps asking for @emotion/react.

Check
grep -rn "styled-engine" webpack.config.js tsconfig.json package.json

Fix

With npm, which has no resolutions field, add a bundler alias and a matching TypeScript paths entry, as the MUI guide shows. With Yarn, point @mui/styled-engine at the sc package in both dependencies and resolutions.

Fix
// webpack.config.js (npm)
module.exports = {
  resolve: {
    alias: {
      '@mui/styled-engine': '@mui/styled-engine-sc',
    },
  },
};

// tsconfig.json (npm)
{
  "compilerOptions": {
    "paths": {
      "@mui/styled-engine": ["./node_modules/@mui/styled-engine-sc"]
    }
  }
}

// package.json (Yarn)
"dependencies": { "@mui/styled-engine": "npm:@mui/styled-engine-sc@latest" },
"resolutions": { "@mui/styled-engine": "npm:@mui/styled-engine-sc@latest" }

Verify

Rebuild; the output no longer mentions Emotion. grep -c should print 0.

Verify
npm run build 2>&1 | grep -c "@emotion"

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • List both Emotion packages as direct dependencies even if no file of yours imports them, because Material UI's default engine loads them.
  • Choose one styling engine per app, and add the alias in the same commit that adds @mui/styled-engine-sc.
  • Use the full install line from the MUI docs in setup scripts and new workspace templates, so a fresh package gets all three packages.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.