库未导入

问题描述

我正在尝试在我的 React Native 项目中导入 react-navigation 库。 但它给了我这个错误

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: IbexCrypto@0.0.1
npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR!   peer react@"*" from @react-navigation/stack@5.14.3
npm ERR!   node_modules/@react-navigation/stack
npm ERR!     @react-navigation/stack@"*" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.0" from @react-native-community/masked-view@0.1.10
npm ERR! node_modules/@react-native-community/masked-view
npm ERR!   peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.3
npm ERR!   node_modules/@react-navigation/stack
npm ERR!     @react-navigation/stack@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict,or retry
npm ERR! this command with --force,or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See C:\Users\me\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\me\AppData\Local\npm-cache\_logs\2021-03-17T15_24_47_867Z-debug.log

当我尝试导入其他一些库时,它也给了我同样的错误。 以前,当我在旧项目中导入导航库时,它工作正常,但现在出现此错误

请告诉我为什么会显示错误。以及如何解决该问题以及如何避免以后出现同样的问题。

如果有人帮助我,我将非常感激,因为我是开发方面的相对初学者,不知道这个错误的逻辑。

解决方法

您有依赖冲突!这意味着您有一个不正确可能损坏的依赖项。 先试试这个:

rm -rf node_modules/
npm install

此命令删除 node_modules 文件夹,然后安装您在 package.json 中的依赖项

如果它不起作用,请尝试使用命令 npm install--force 运行 --legacy-peer-deps

就您而言: npm install @react-navigation/native --force

或:npm install @react-navigation/native --legacy-peer-deps

命令说明:

--force:强制 npm install 安装您要安装的依赖项。

-legacy-peer-deps: --legacy-peer-deps: 安装时忽略所有 peerDependencies。

这些命令在某些情况下是必需的,但作为标准 npm install 应该足以从您的 package.json 文件安装依赖项。

,

使用通用

npm install @react-navigation/native

用于导入 react-navigation 库

,

对于您的问题,这两个命令可能有效

npm install --legacy-peer-deps

npm install --force

版本 3 和 4 中的 React 导航命令

npm install —-save react-navigation

导航有一些小的变化 4 React navigaiton

在版本 5 中响应导航命令

npm install @react-navigation/native

你可以像下面的代码一样导入

import { createAppContainer } from "react-navigation";
,

请参考官方文档进行 react-navigation 并从这里安装所有必需的依赖项: https://reactnavigation.org/docs/getting-started

安装依赖后运行

react-native start --reset-cache

然后

react-native run-android