13

I'm trying to use the mui-v5 with styled-components and typescript. However, even after I added the line to my tsconfig:

"paths": {
  "@mui/styled-engine": [ "./node_modules/@mui/styled-engine-sc" ]
}

I'm getting an error:

Could not find dependency: '@emotion/styled' relative to '/node_modules/@mui/styled-engine/index.js'

when doing: import { ThemeProvider } from "@mui/material/styles";

I made a Code Sandbox to show my setup.

Does anyone know what I'm doing wrong?

1
  • Do you mind showing the content of your package.json? I have "@mui/styled-engine-sc in my deps and everything is working fine. Commented Mar 23, 2022 at 21:42

2 Answers 2

26

You need to add @emotion/styled and @emotion/react to dependencies

Sign up to request clarification or add additional context in comments.

6 Comments

But I thought the whole point of using styled-components was that I don't need emotion
Yeah I checked this mui.com/guides/styled-engine it says that @emotion/react, @emotion/styled, and styled-components are optional peer dependencies of @mui/material, so you need to install them yourself.
Those instructions are really confusing. If it's optional doesn't that mean you don't need them?
Check if this codesandbox is what you are looking for codesandbox.io/s/styled-components-forked-4f88k?file=/src/…
finally. I've read multi para solutions that didn't fix it. this did, thank you
|
11

You don't need to add @emotion/styled. The mui installation says you can use emotion OR styled-components

I know the instructions are a bit confusing but I just copy/pasted the package.json portion in mui.com/guides/styled-engine/ and it worked for me

package.json:

{
  "dependencies": {
    "@mui/material": "^5.0.0",
    "styled-components": "^5.3.1",
  },
  "devDependencies": {
    "@mui/styled-engine": "npm:@mui/styled-engine-sc@latest",
    "@types/styled-components": "^5.1.14",
  },
  "resolutions": {
    "@mui/styled-engine": "npm:@mui/styled-engine-sc@latest"
  }
}

2 Comments

The problem is that this only works with yarn, since "resolutions" is a yarn thing
Thank you! How does one figure this out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.