找不到模块:无法解决“反应图标”错误
Module not found: Can’t resolve ‘react-icons’ error
要解决错误“找不到模块:错误:无法解析’react-icons’”,请确保react-icons
通过在项目的根目录中打开终端并运行命令npm install react-icons
并重新启动开发服务器来安装软件包。
在项目的根目录(package.json
文件所在的位置)中打开终端并运行以下命令:
# 👇️ with NPM npm install react-icons # ---------------------------------------------- # 👇️ with YARN yarn add react-icons
该命令会将
react-icons包添加到项目的依赖项中。
npm start
您现在应该能够react-icons
在 React.js 应用程序中导入和使用该包。
import {FaBeer} from 'react-icons/fa'; function App() { return ( <div> <p> Hello world <FaBeer /> </p> </div> ); } export default App;
如果错误未解决,请尝试删除您的node_modules
和
package-lock.json
(不是package.json
)文件,重新运行npm install
并重新启动您的 IDE。
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
如果您仍然收到“找不到模块:错误:无法解析‘react-icons’”错误,请打开您的package.json
文件并确保它包含对象中的react-icons
包dependencies
。
{ // ... rest "dependencies": { "react-icons": "^4.3.1", }, }
The react-icons
module should NOT be globally installed or be in your
project’s devDependencies
, it should be in the dependencies
object in your
package.json
file.
You can try to manually add the lines and re-run npm install
.
npm install
Or install the latest version of the package:
npm install react-icons@latest
Conclusion #
To solve the error “Module not found: Error: Can’t resolve ‘react-icons'”,
make sure to install the react-icons
package by opening your terminal in your
project’s root directory and running the command npm install react-icons
and
restart your dev server.