You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-native依赖冲突:npm install执行失败求助

问题描述
  • 执行命令 cd core && npm ci 时触发ERESOLVE错误:解析react-native-fs@2.18.0过程中,项目依赖的react@17.0.2与react-native-windows@0.70.5要求的react@18.1.0版本冲突。
  • 尝试使用 npm install --legacy-peer-deps 绕过冲突后,出现「✖ Failed to build definition files」类型构建错误。

项目依赖配置如下:

{
  "dependencies": {
    "@aries-framework/core": "^0.2.2",
    "@aries-framework/react-hooks": "^0.3.0",
    "@aries-framework/react-native": "^0.2.0",
    "@formatjs/intl-datetimeformat": "^4.2.3",
    "@formatjs/intl-displaynames": "^5.2.3",
    "@formatjs/intl-getcanonicallocales": "^1.7.3",
    "@formatjs/intl-listformat": "^6.3.3",
    "@formatjs/intl-locale": "^2.4.37",
    "@formatjs/intl-numberformat": "^7.2.3",
    "@formatjs/intl-pluralrules": "^4.1.3",
    "@formatjs/intl-relativetimeformat": "^9.2.3",
    "@react-native-async-storage/async-storage": "^1.12.1",
    "@react-native-community/masked-view": "^0.1.11",
    "@react-native-community/netinfo": "^9.3.0",
    "@react-navigation/bottom-tabs": "^6.0.9",
    "@react-navigation/core": "^6.1.0",
    "@react-navigation/devtools": "^6.0.8",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/stack": "^6.0.11",
    "axios": "^0.21.0",
    "i18next": "^21.4.0",
    "indy-sdk-react-native": "^0.2.2",
    "lodash.flatten": "^4.4.0",
    "lodash.startcase": "^4.4.0",
    "query-string": "^7.0.1",
    "react": "17.0.2",
    "react-i18next": "^11.13.0",
    "react-native": "0.66.5",
    "react-native-animated-pagination-dots": "^0.1.72",
    "react-native-argon2": "^2.0.1",
    "react-native-bouncy-checkbox": "^3.0.5",
    "react-native-camera": "^3.31.1",
    "react-native-collapsible": "^1.6.0",
    "react-native-config": "^1.4.2",
    "react-native-confirmation-code-field": "^7.3.0",
    "react-native-device-info": "^8.4.9",
    "react-native-encrypted-storage": "^4.0.2",
    "react-native-fs": "^2.16.6",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-get-random-values": "^1.7.0",
    "react-native-gifted-chat": "^0.16.3",
    "react-native-keychain": "^8.1.1",
    "react-native-localize": "^2.1.5",
    "react-native-qrcode-svg": "^6.0.6",
    "react-native-reanimated": "^2.2.4",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-scalable-image": "^1.1.0",
    "react-native-screens": "^3.3.0",
    "react-native-splash-screen": "^3.2.0",
    "react-native-svg": "^12.1.1",
    "react-native-toast-message": "^2.0.1",
    "react-native-uuid": "^2.0.1",
    "react-native-vector-icons": "^8.1.0",
    "uuid": "^8.1.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@commitlint/cli": "^11.0.0",
    "@react-native-community/eslint-config": "^2.0.0",
    "@testing-library/jest-native": "^4.0.4",
    "@testing-library/react-native": "^9.0.0",
    "@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.18",
    "@types/jest": "^27.4.0",
    "@types/lodash.flatten": "^4.4.6",
    "@types/lodash.startcase": "^4.4.6",
    "@types/react": "^16.14.4",
    "@types/react-native": "^0.63.50",
    "@types/react-native-vector-icons": "^6.4.6",
    "@types/react-test-renderer": "^16.9.5",
    "@typescript-eslint/eslint-plugin": "^4.20.0",
    "@typescript-eslint/parser": "^4.33.0",
    "babel-jest": "^27.5.1",
    "babel-plugin-module-resolver": "^4.1.0",
    "commitlint": "^11.0.0",
    "eslint": "^7.14.0",
    "eslint-import-resolver-typescript": "^2.5.0",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-prettier": "^3.3.1",
    "husky": "^7.0.0",
    "jest": "^27.5.1",
    "lint-staged": "^11.2.6",
    "metro-react-native-babel-preset": "^0.66.2",
    "mockdate": "^3.0.5",
    "node-notifier": "^10.0.1",
    "patch-package": "^6.4.4",
    "prettier": "^2.1.2",
    "react-native-builder-bob": "^0.18.2",
    "react-native-flipper": "^0.154.0",
    "react-native-svg-transformer": "^0.14.3",
    "react-test-renderer": "17.0.2",
    "ts-jest": "^27.1.3",
    "typescript": "^4.2.2"
  }
}
解决方案

1. 锁定react-native-fs版本,避免自动升级

冲突根源是react-native-fs从指定的^2.16.6自动升级到2.18.0,间接引入了高版本依赖:

  • 修改package.json中react-native-fs的版本为"react-native-fs": "2.16.6"(去掉版本号前的^)
  • 删除core目录下的package-lock.json文件
  • 重新执行cd core && npm ci

2. 修复类型构建错误(适配legacy-peer-deps)

如果使用--legacy-peer-deps绕过冲突后出现类型构建失败,需要同步类型依赖版本:

  • 将devDependencies中的@types/react升级为与react@17.0.2匹配的版本:"@types/react": "^17.0.47"
  • 将@types/react-native升级为与react-native@0.66.5匹配的版本:"@types/react-native": "^0.66.20"
  • 清理缓存后重新安装:
    npm cache clean --force
    rm -rf node_modules
    cd core && npm install --legacy-peer-deps
    

3. 使用npm override强制统一react版本

在core目录的package.json中添加overrides字段,强制所有依赖使用react@17.0.2:

"overrides": {
  "react": "17.0.2",
  "react-native-windows": {
    "react": "17.0.2"
  }
}

添加后执行cd core && npm ci,该方法会强制忽略react-native-windows对react的高版本要求,适合无法升级项目react版本的场景。

4. 移除react-native-windows依赖(若无需Windows支持)

如果项目不需要适配Windows平台,可排查间接引入react-native-windows的依赖包,替换为不包含该依赖的版本,或者直接在package.json中排除该依赖。

内容的提问来源于stack exchange,提问作者Sam O'Neill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:35:18