javascript 找不到模块:无法解析“@material-ui/core/Container”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56230568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Module not found: Can't resolve '@material-ui/core/Container'
提问by Jessica
In the browser, I get the error
在浏览器中,我收到错误
Failed to compile Module not found: Can't resolve '@material-ui/core/Container'
无法编译模块未找到:无法解析“@material-ui/core/Container”
It's looking for the component inside of my components directory instead of node_modules. I can't change directories into node_modules ../../because node_modules is outside of srcdirectory and Create React App won't let me.
它正在我的组件目录中寻找组件而不是 node_modules。我无法将目录更改为 node_modules,../../因为 node_modules 在src目录之外并且 Create React App 不允许我。
I've use yarn to remove and $ yarn add @material-ui/core. I've tried yarn run build which gives me the error
我已经使用 yarn 删除了 和$ yarn add @material-ui/core。我试过 yarn run build 这给了我错误
Cannot find module: '@material-ui/core/Container'. Make sure this package is installed. You can install this package by running: yarn add @material-ui/core/Container.
找不到模块:'@material-ui/core/Container'。确保安装了这个包。你可以通过运行来安装这个包:yarn add @material-ui/core/Container。
When I try to add it, I get the error
当我尝试添加它时,出现错误
error Couldn't find package "@material-ui/core/Container" on the "npm" registry.
错误在“npm”注册表中找不到包“@material-ui/core/Container”。
Here's the dependencies I have that are related:
这是我拥有的相关依赖项:
"@material-ui/core": "^3.9.3",
"@material-ui/icons": "^3.0.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
I expect to see the contents of the page not take up the full width of the screen, but instead, I receive a fail to compile error.
我希望看到页面的内容不会占据屏幕的整个宽度,但相反,我收到了编译失败错误。
回答by Ezra
Containeris not part of the material-uiversion specified in your package.json.
Container不是material-ui您的package.json.
To upgrade, run the following:
要升级,请运行以下命令:
$ yarn add @material-ui/core@next
You might have to remove the old stable version (if that's even an option for you).
您可能需要删除旧的稳定版本(如果这对您来说是一个选项)。
React and react-dom >= 16.8.0 are all that are needed as peer dependencies, so the experimental upgrade of material-uishould be all you need to use Container.
React 和 react-dom >= 16.8.0 都是对等依赖所需要的,所以实验性升级material-ui应该是你需要使用的全部Container。
回答by Chase Owens
If you follow a npx create-react-app new-appwith cd new-appand yarn add @material-ui/coreit wile compile with yarn start. You might try just starting over.
如果您遵循npx create-react-app new-appwithcd new-app并且yarn add @material-ui/core它会用yarn start. 您可以尝试重新开始。
However, to help your troubleshooting, this error typically happens when you try to use a Material-UI component and forget to import it. Have you tried commenting out all the code and seeing if it compiles?
但是,为了帮助您进行故障排除,当您尝试使用 Material-UI 组件而忘记导入它时,通常会发生此错误。您是否尝试过注释掉所有代码并查看它是否可以编译?

