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

执行npm install遇ERESOLVE依赖冲突,--legacy-peer-deps无效求助

问题描述

Windows 10系统下,克隆仓库后执行npm install安装依赖时,终端报错如下:

npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: react-scripts@4.0.0
npm ERR! Found: typescript@4.1.3
npm ERR! node_modules/typescript
npm ERR! dev typescript@"4.1.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peerOptional typescript@"^3.2.1" from react-scripts@4.0.0
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"4.0.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: typescript@3.9.10
npm ERR! node_modules/typescript
npm ERR! peerOptional typescript@"^3.2.1" from react-scripts@4.0.0
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"4.0.0" 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 C:\Users\USER-PC\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\USER-PC\AppData\Local\npm-cache_logs\2022-10-04T10_18_32_

即使执行npm install --legacy-peer-deps命令,仍出现相同错误。

当前项目的package.json内容:

}
 ]
  "name": "info-site",
  "version": "1.0.0",
  "description": "",
  "keywords": [],
  "main": "src/index.js",
  "dependencies": {
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-scripts": "4.0.0"
  },
  "devDependencies": {
    "@babel/runtime": "7.13.8",
    "typescript": "4.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}
可能的问题及解决办法
  • package.json语法错误:你的package.json开头存在无效语法(第一行是},第二行是 ]),这会导致npm无法正确解析整个依赖配置,也是--legacy-peer-deps命令失效的关键原因。必须先修正这个错误,删除开头的}和 ],确保文件以{开头,JSON结构完全合法。

  • 依赖版本兼容性冲突:

    • react-scripts@4.0.0的可选peer依赖要求typescript版本在^3.2.1范围内,但你指定的是typescript@4.1.3,版本超出范围引发冲突。
    • react@18.2.0和react-dom@18.2.0是较新的版本,而react-scripts@4.0.0并不支持React 18,这会带来更多潜在的兼容问题。
  • 具体修复步骤:

    1. 修正package.json的语法错误,确保文件结构正确。
    2. 更新依赖版本到兼容组合,比如将react-scripts升级到支持React 18的5.0.1,同时调整相关依赖:
      {
        "name": "info-site",
        "version": "1.0.0",
        "description": "",
        "keywords": [],
        "main": "src/index.js",
        "dependencies": {
          "react": "18.2.0",
          "react-dom": "18.2.0",
          "react-scripts": "5.0.1"
        },
        "devDependencies": {
          "@babel/runtime": "7.22.10",
          "typescript": "4.9.5"
        },
        "scripts": {
          "start": "react-scripts start",
          "build": "react-scripts build",
          "test": "react-scripts test --env=jsdom",
          "eject": "react-scripts eject"
        },
        "browserslist": [
          ">0.2%",
          "not dead",
          "not ie <= 11",
          "not op_mini all"
        ]
      }
      
    3. 删除项目根目录下的node_modules文件夹和package-lock.json文件。
    4. 重新执行npm install,如果仍有问题可尝试npm install --legacy-peer-deps。

内容的提问来源于stack exchange,提问作者michy David

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 07:55:20