找不到模块:无法解析“E:\frontend\node_modules\@mui\styled-engine”中的“@emotion/react”

stm*_*lee 40 javascript npm reactjs material-ui

我正在尝试从 导入一个盒子组件@mui/material/Box。我已经使用以下命令安装了 Material-UI 5 npm i @mui/material。但出现这个错误,Module not found: Can't resolve '@emotion/react' in 'E:\frontend\node_modules\@mui\styled-engine'

这是我的代码。

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>
  );
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?

Pet*_*man 63

@emotion/react@emotion/styled是许多mui包的对等依赖项,即@mui/material. 这让我们可以更自由地选择要使用的情感版本,但这确实意味着我们必须自己安装依赖项。

npm i @emotion/react @emotion/styled
Run Code Online (Sandbox Code Playgroud)

  • 为什么我们需要这样做?在没有我帮助的情况下,material ui 不应该安装它的所有依赖项吗? (24认同)
  • 这是一个很好的观点,社区正在积极讨论这个[问题](https://github.com/mui-org/material-ui/pull/22435),两种方式都需要权衡。 (3认同)
  • 在入门文档中提及这一点可能会更好。 (3认同)