由于依赖关系无法升级 React-Native 项目

atl*_*der 5 npm react-native

我正在尝试将我的 RN 项目从 0.62.2 更新到最新版本(当前是 0.63.3)。我只是在运行npm update并收到错误:

npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR!   react@"^17.0.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"16.13.1" from react-native@0.63.3
npm ERR! node_modules/react-native
npm ERR!   react-native@"^0.63.3" 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.
Run Code Online (Sandbox Code Playgroud)

在跑步之前,npm update我跑过npx npm-check-updates -u并得到了以下信息:

 @react-native-community/netinfo          ^5.9.5  ?   ^5.9.9     
 @react-navigation/drawer                 ^5.8.6  ?  ^5.11.4     
 @react-navigation/native                 ^5.7.1  ?  ^5.8.10     
 @react-navigation/stack                  ^5.7.1  ?  ^5.12.8     
 @types/react                           ^16.9.43  ?  ^17.0.0     
 amazon-cognito-identity-js               ^3.3.3  ?   ^4.5.5     
 aws-amplify                             ^3.0.21  ?  ^3.3.10     
 axios                                   ^0.19.2  ?  ^0.21.0     
 graphql                                 ^0.11.7  ?  ^15.4.0     
 graphql-tag                             ^2.10.4  ?  ^2.11.0     
 moment-timezone                         ^0.5.31  ?  ^0.5.32     
 react                                  ^16.11.0  ?  ^17.0.1     
 react-native                            ^0.62.2  ?  ^0.63.3     
 react-native-base64                       0.0.2  ?    0.1.0     
 react-native-gesture-handler             ~1.5.0  ?   ~1.9.0     
 react-native-reanimated                  ~1.4.0  ?  ~1.13.2     
 react-native-safe-area-context           ^0.7.3  ?   ^3.1.9     
 react-native-screens             2.0.0-alpha.12  ?   2.15.0     
 react-native-simple-crypto              ^0.2.13  ?  ^0.2.15     
 react-native-vector-icons                ^6.7.0  ?   ^7.1.0     
 react-navigation                         ^4.4.0  ?   ^4.4.3     
 @babel/core                               7.8.7  ?   7.12.9     
 @babel/runtime                            7.8.7  ?   7.12.5     
 babel-jest                               24.9.0  ?   26.6.3     
 metro-react-native-babel-preset          0.56.4  ?   0.64.0     
 react-test-renderer                      16.9.0  ?   17.0.1   
Run Code Online (Sandbox Code Playgroud)

好的,我总结了错误react@^17.0.1的错误react-native@0.63.3,需要react@16.13.1但这是由npx npm-check-updates -u- 为什么它使 deps 树不兼容?..

如果我将手动反应版本更改为“^16.0.0”,则会npm install失败:

npm ERR! Found: react@16.14.0
npm ERR! node_modules/react
npm ERR!   react@"^16.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"16.13.1" from react-native@0.63.3
npm ERR! node_modules/react-native
npm ERR!   react-native@"^0.63.3" from the root project
Run Code Online (Sandbox Code Playgroud)

为什么是“react@16.14.0”?!来自哪里?

我只是不明白这一切是如何工作的……请提出建议。

我的当前package.json

{
  "name": "Proj",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^5.9.9",
    "@react-navigation/drawer": "^5.11.4",
    "@react-navigation/native": "^5.8.10",
    "@react-navigation/stack": "^5.12.8",
    "@types/react": "^17.0.0",
    "amazon-cognito-identity-js": "^4.5.5",
    "aws-amplify": "^3.3.10",
    "axios": "^0.21.0",
    "graphql": "^15.4.0",
    "graphql-tag": "^2.11.0",
    "i18n-js": "^3.8.0",
    "moment-timezone": "^0.5.32",
    "react": "^16.0.0",
    "react-native": "^0.63.3",
    "react-native-avatar-social": "0.0.3",
    "react-native-base64": "0.1.0",
    "react-native-gesture-handler": "~1.9.0",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-localize": "^2.0.1",
    "react-native-reanimated": "~1.13.2",
    "react-native-safe-area-context": "^3.1.9",
    "react-native-screens": "2.15.0",
    "react-native-simple-crypto": "^0.2.15",
    "react-native-tab-view": "^2.15.2",
    "react-native-tab-view-viewpager-adapter": "^1.1.0",
    "react-native-uuid-generator": "^6.1.1",
    "react-native-vector-icons": "^7.1.0",
    "react-native-video": "^4.4.5",
    "react-native-viewpager": "^0.2.13",
    "react-native-zeroconf": "github:atlascoder/react-native-zeroconf",
    "react-navigation": "^4.4.3",
    "rn-fetch-blob": "^0.12.0"
  },
  "devDependencies": {
    "@babel/core": "7.12.9",
    "@babel/runtime": "7.12.5",
    "babel-jest": "26.6.3",
    "metro-react-native-babel-preset": "0.64.0",
    "react-test-renderer": "17.0.1"
  },
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

更新

按照@anthony-marino 的建议,我通过更新助手手动更新了依赖项,然后我又回到了让我升级的问题!

npm ERR! Found: react@16.13.1
npm ERR! node_modules/react
npm ERR!   react@"16.13.1" from the root project
npm ERR!   peer react@"^16.0" from @react-native-community/viewpager@4.2.1
npm ERR!   node_modules/@react-native-community/viewpager
npm ERR!     peer @react-native-community/viewpager@"*" from react-native-tab-view-viewpager-adapter@1.1.0
npm ERR!     node_modules/react-native-tab-view-viewpager-adapter
npm ERR!       react-native-tab-view-viewpager-adapter@"^1.1.0" from the root project
npm ERR!   15 more (@react-navigation/core, @react-navigation/drawer, ...)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"16.11.0" from react-native-zeroconf@0.12.3
npm ERR! node_modules/react-native-zeroconf
npm ERR!   react-native-zeroconf@"github:atlascoder/react-native-zeroconf" from the root project
npm ERR! 
Run Code Online (Sandbox Code Playgroud)

而让我筋疲力尽的事情..

我有一个组件from @react-native-community/viewpager,为什么它需要 peerDep react@"^16.0"- 为什么它不接受react@16.13.1. 这^16.0意味着更新鲜16.*,不是吗?!当我检查^16.0NPM semver计算器-它显示我16.13.1为合适..

变通方法

在更新时遇到这个麻烦:

  1. 删除node_modulespackage-lock.json
  2. npm i
  3. npm i失败-删除失败的依赖
  4. 完成后npm i- 手动安装删除的依赖项

Chr*_*way -1

从您的日志中:react-native 0.63.3需要react@“16.13.1”,但找到react@“^17.0.1”。只需降级你的反应npm install react@16.13.1,然后重试npm install。它应该可以正常工作,不会出现错误。如果你运行 npm outdated 你会发现“react”已经过时了。但您不太可能拥有最新版本的所有软件包。