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

expect@29.6.1与Node版本不兼容问题求助(无法升级Node)

解决expect@29.6.1与Node.js 12.22.12不兼容问题

问题描述

运行项目时触发以下错误:

error expect@29.6.1: The engine "node" is incompatible with this module. Expected version "^14.15.0 || ^16.10.0 || >=18.0.0". Got "12.22.12"        
error Found incompatible module.

expect库由jest引入,本地node_modules中仅存在expect@26.6.2,但错误由未在项目中显式依赖的expect@29.6.1触发。由于升级Node.js会影响其他依赖,无法升级,需解决该版本冲突问题。

已执行的依赖检查

执行命令查看expect依赖链:

npm ls expect

返回结果:

yarn why v1.17.3
[1/4] Why do we have the module "expect"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "expect@26.6.2"
info Has been hoisted to "expect"
info Reasons this module exists
   - "workspace-aggregator-0ca060b9-5530-4147-845d-6bce9cd2005f" depends on it
   - Hoisted from "_project_#jest-snapshot#expect"
   - Hoisted from "_project_#jest-runtime#@jest#globals#expect"
   - Hoisted from "_project_#jest-config#jest-jasmine2#expect"
info Disk size without dependencies: "244KB"
info Disk size with unique dependencies: "1.55MB"
info Disk size with transitive dependencies: "7.34MB"
info Number of shared dependencies: 68
=> Found "jest-circus#expect@27.5.1"
info Reasons this module exists
   - "_project_#jest-circus" depends on it
   - Hoisted from "_project_#jest-circus#jest-snapshot#expect"
   - Hoisted from "_project_#jest-circus#jest-runtime#@jest#globals#expect"
info Disk size without dependencies: "236KB"
info Disk size with unique dependencies: "1.45MB"
info Disk size with transitive dependencies: "7.32MB"
info Number of shared dependencies: 67
=> Found "jest#expect@27.4.6"
info Reasons this module exists
   - "_project_#jest#@jest#core#jest-snapshot" depends on it
   - Hoisted from "_project_#jest#@jest#core#jest-snapshot#expect"
   - Hoisted from "_project_#jest#@jest#core#jest-runtime#@jest#globals#expect"
   - Hoisted from "_project_#jest#@jest#core#jest-config#jest-jasmine2#expect"
info Disk size without dependencies: "236KB"
info Disk size with unique dependencies: "1.45MB"
info Disk size with transitive dependencies: "7.32MB"
info Number of shared dependencies: 67
=> Found "jest-cli#expect@27.4.6"
info Reasons this module exists
   - "_project_#jest#jest-cli#jest-config#jest-jasmine2" depends on it
   - Hoisted from "_project_#jest#jest-cli#jest-config#jest-jasmine2#expect"
   - Hoisted from "_project_#jest#jest-cli#@jest#core#jest-snapshot#expect"
   - Hoisted from "_project_#jest#jest-cli#@jest#core#jest-runtime#@jest#globals#expect"
info Disk size without dependencies: "236KB"
info Disk size with unique dependencies: "1.45MB"
info Disk size with transitive dependencies: "7.32MB"
info Number of shared dependencies: 67
Done in 1.74s.

解决方案

1. 强制锁定expect兼容版本

通过依赖覆盖机制,强制所有子依赖使用兼容Node.js 12的expect版本:

  • Yarn用户:在package.json中添加resolutions字段:
    "resolutions": {
      "expect": "^26.6.2"
    }
    
  • npm 8.3+用户:在package.json中添加overrides字段:
    "overrides": {
      "expect": "^26.6.2"
    }
    

添加后重新安装依赖:

# Yarn
yarn install

# npm
npm install

2. 清理缓存并重装依赖

缓存可能残留不兼容版本,执行以下步骤清理:

# Yarn流程
yarn cache clean
rm -rf node_modules
yarn install

# npm流程
npm cache clean --force
rm -rf node_modules package-lock.json
npm install

3. 排查隐性依赖来源

检查是否有外部因素引入高版本expect:

  • 确认全局安装的jest或其他工具是否干扰本地依赖
  • 查看CI/CD脚本或项目scripts中是否有修改依赖的命令
  • 检查是否有未提交的package.json/yarn.lock/package-lock.json变更

4. 临时跳过引擎检查(应急方案)

若上述方法无效,可临时跳过Node版本验证,但可能带来潜在风险:

# Yarn
yarn install --ignore-engines

# npm
npm install --ignore-engines

内容的提问来源于stack exchange,提问作者Aaditya Jha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 06:17:00