合并分支后develop环境出现selenium-webdriver与Node版本不兼容问题
问题场景
在基于Expo的React Native项目中,我完成了开发分支的依赖整理,GitLab流水线也已通过,但将该分支合并到develop分支后,执行yarn命令时出现以下Node版本不兼容错误:
error selenium-webdriver@4.5.0: The engine "node" is incompatible with this module. Expected version ">= 14.20.0". Got "14.17.4" error Found incompatible module.
此前在其他分支遇到该错误时,升级Node到v14.20.0却引发大量TypeScript问题;使用nohoist解决模块安装路径问题后回退到Nodev14.17.4,原分支可正常运行,但合并后的develop分支无法工作。另外,项目中无selenium-webdriver的直接引用,完全不清楚该依赖的来源。
项目根目录package.json内容如下:
{ "name": "oml", "private": true, "scripts": { "start": ": You are in the project root. cd into the relevant package and run yarn start to start that package.", "prepare": "husky install", "docs": "yarn workspace @oml/types docs", "test:commit": "yarn workspaces run test:commit", "test:ci": "yarn workspaces run test:ci", "check-code": "yarn workspaces run check-code", "build:web": "cd apps/trader-portal && expo build:web --non-interactive && rm -rf ../firebase/web-build && mv web-build ../firebase/", "build:android": "cd apps/trader-portal && expo build:android -t app-bundle --non-interactive --no-wait --release-channel", "build:ios": "cd apps/trader-portal && expo build:ios --non-interactive --no-wait --release-channel" }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^4.29.3", "@typescript-eslint/parser": "^4.29.3", "concurrently": "^6.3.0", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.24.2", "eslint-plugin-jest": "^24.4.0", "eslint-plugin-react": "^7.26.0", "eslint-plugin-react-hooks": "^4.2.0", "eslint-plugin-react-native": "^3.11.0", "husky": "^7.0.4", "open-cli": "^7.0.1", "prettier": "2.3.2", "typescript": "4.8.4" }, "workspaces": { "packages": [ "./packages/*", "./apps/**" ], "nohoist": [ "**/@react-native-community", "**/@react-native-community/**" ] } }
解决方案
1. 定位间接依赖来源
先执行命令找出selenium-webdriver的引入路径:
yarn why selenium-webdriver
该命令会输出完整依赖链,明确是哪个子依赖(如测试工具、自动化脚本类包)间接引入了它。
2. 强制指定兼容版本
在根目录package.json中添加resolutions字段,强制使用兼容Nodev14.17.4的selenium-webdriver版本(比如4.4.0,需确认该版本支持Node>=14.17.4):
{ // 保留原有字段 "resolutions": { "selenium-webdriver": "4.4.0" } }
添加后重新执行yarn install,该方法会让所有依赖链统一使用指定版本,规避版本冲突。
3. 调整直接依赖版本
如果是某个直接依赖(如测试库)拉取了高版本selenium-webdriver,可尝试降低该直接依赖的版本,使其引入兼容当前Node版本的selenium-webdriver。
4. 临时应急方案(不推荐长期使用)
若上述方法暂时无法生效,可临时跳过yarn的引擎检查:
yarn install --ignore-engines
注意:该方法仅绕开错误提示,可能隐藏其他兼容性问题,仅限应急使用。
5. 检查workspace子包依赖
由于项目使用yarn workspace,合并分支后可能某个子包(packages/*或apps/**)的依赖发生变更,需逐个检查子包的package.json,确认是否新增了会拉取selenium-webdriver的依赖。
内容的提问来源于stack exchange,提问作者TreyCollier

