DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 pnpm 报错 unmet peer dependency strict resolution 幽灵依赖修复

排查 pnpm 严格模式下执行安装时大量警告或失败,报 ERR_PNPM_PEER_DEP_ISSUES: Unmet peer dependency: react@^18.0.0,分析 硬链接虚拟存储结构与 peerDependencies 语义化规则。

问题现象

在大型 Monorepo 根目录下运行 pnpm install 时,安装进程抛出严格 peer 依赖不满足错误并退出:

ERR_PNPM_PEER_DEP_ISSUES: Unmet peer dependencies

. (root)
└─┬ my-ui-lib 1.0.0
  └── ✕ unmet peer react@"^18.0.0": found 17.0.2 in root

触发根因

  1. pnpm 默认采用严格符号链接(Symlink)与内容寻址存储(Content-addressable Store)模型,坚决杜绝 npm/yarn 的幽灵依赖(Phantom Dependencies)隐患;
  2. 第三方库声明的 peerDependencies 与主应用或 Monorepo 根依赖声明的版本区间存在硬性冲突;
  3. 未配置自动化安装或忽略规则导致严格模式拦截。

解决步骤

在项目根目录 .npmrc 文件中开启自动补齐与宽容规则

编辑 .npmrc:

auto-install-peers=true
strict-peer-dependencies=false

针对已知兼容但版本声明滞后的库配置忽略名单

在 package.json 中定义 pnpm.peerDependencyRules:

{
  "pnpm": {
    "peerDependencyRules": {
      "ignoreMissing": ["react", "react-dom"],
      "allowedVersions": {
        "react": "17 || 18"
      }
    }
  }
}

使用 pnpm audit 与 dedupe 清理死锁版本

保持依赖关系干净有序。

验证确认

重新执行 pnpm install:

所有包依赖毫秒级完成硬链接映射,依赖树整洁无警告,Monorepo 顺畅构建。