Git Action中npm install失败但本地正常的问题求助
问题:GitHub Actions中npm install失败,本地运行正常
环境与配置
本地环境
- Node版本:v16.3.0
- 删除
package-lock.json后仍可正常执行npm install
GitHub Actions配置
name: Check for lint/build errors on: pull_request: branches: - '**' jobs: lint: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 with: submodules: recursive token: ${{ secrets.SEMANTIC_RELEASE_PAT }} - uses: actions/setup-node@v2 with: node-version: 16 registry-url: https://registry.npmjs.org/ - name: Set NPM_TOKEN run: echo "NPM_TOKEN=${{ secrets.NPM_TOKEN }}" >> $GITHUB_ENV - name: npm install, lint run: | npm install npm run lint
错误信息
npm ERR! While resolving: @react-native-async-storage/async-storage@1.15.9 npm ERR! Found: react-native@0.68.2 npm ERR! node_modules/react-native npm ERR! react-native@"0.68.2" from the root project npm ERR! peer react-native@">=0.64.0-rc.0 || 0.0.0-*" from @react-native-community/cli@5.0.1 npm ERR! node_modules/@react-native-community/cli npm ERR! dev @react-native-community/cli@"^5.0.1" from the root project npm ERR! 11 more (react-native-actions-sheet, react-native-check-box, ...) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer react-native@"^0.0.0-0 || ^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63.2 || ^0.64.0 || ^0.65.0 || ^0.66.0 || 1000.0.0" from @react-native-async-storage/async-storage@1.15.9 npm ERR! node_modules/@react-native-async-storage/async-storage npm ERR! @react-native-async-storage/async-storage@"^1.15.4" from the root project npm ERR! npm ERR! Conflicting peer dependency: react-native@0.66.5 npm ERR! node_modules/react-native npm ERR! peer react-native@"^0.0.0-0 || ^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63.2 || ^0.64.0 || ^0.65.0 || ^0.66.0 || 1000.0.0" from @react-native-async-storage/async-storage@1.15.9 npm ERR! node_modules/@react-native-async-storage/async-storage npm ERR! @react-native-async-storage/async-storage@"^1.15.4" 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 /home/runner/.npm/eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! /home/runner/.npm/_logs/2022-12-30T10_36_18_687Z-debug-0.log
原因分析
核心差异在于npm版本的peer依赖处理策略不同:
- 本地Node v16.3.0对应npm v7.15.1,该版本虽启用peer依赖检查,但对部分冲突的容忍度更高,无lock文件时会自动尝试兼容处理;
- GitHub Actions中
setup-node@v2指定node-version:16时,会安装该大版本的最新稳定版(如v16.20.x),对应npm v8.x。npm 8对peer依赖冲突的检查更严格,一旦检测到依赖包要求的peer版本范围与当前项目版本不兼容,直接抛出错误终止安装。
具体到案例:@react-native-async-storage/async-storage@1.15.9要求的react-native版本范围不包含项目使用的0.68.2,npm 8严格执行规则导致失败,而本地npm 7则允许继续安装。
解决方法
方法1:添加--legacy-peer-deps参数
在Actions的npm install命令中加入该参数,恢复npm 6的peer依赖处理逻辑,绕过严格检查:
- name: npm install, lint run: | npm install --legacy-peer-deps npm run lint
方法2:锁定Node/npm版本与本地一致
在setup-node步骤中指定和本地完全相同的Node版本,确保环境一致:
- uses: actions/setup-node@v2 with: node-version: '16.3.0' registry-url: https://registry.npmjs.org/
方法3:升级依赖包(推荐)
将@react-native-async-storage/async-storage升级到支持react-native@0.68.2的版本(如v1.17.x及以上版本已兼容RN 0.68+),更新package.json中的依赖范围后重新生成package-lock.json:
"@react-native-async-storage/async-storage": "^1.17.0"
提交更新后的package.json和package-lock.json到仓库,Actions即可正常执行npm install。
内容的提问来源于stack exchange,提问作者Alwaysblue
相关产品推荐
相关产品推荐

