npm

安装 npm install http-server -g
运行 http-server -p 端口号

npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.0.0" from @ant-design/cssinjs@1.24.0 npm warn node_modules/@ant-design/cssinjs npm warn @ant-design/cssinjs@"^1.18.4" from the root project npm warn 8 more (@umijs/plugins, antd, @ant-design/pro-card, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.0.0" from @ant-design/icons@5.6.1 npm warn node_modules/@ant-design/icons npm warn @ant-design/icons@"^5.2.6" from the root project npm warn 8 more (antd, @ant-design/pro-card, @ant-design/pro-field, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/utilities@3.2.2 npm warn node_modules/@dnd-kit/utilities npm warn @dnd-kit/utilities@"^3.2.2" from the root project npm warn 4 more (@dnd-kit/modifiers, @dnd-kit/sortable, @dnd-kit/core, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: peer react@"15.x || ^16.0.0-0" from the root project npm warn npm warn Could not resolve dependency: npm warn peer react@"15.x || ^16.0.0-0" from the root project npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react-dom@undefined npm warn node_modules/react-dom npm warn peer react-dom@"15.x || ^16.0.0-0" from the root project npm warn npm warn Could not resolve dependency: npm warn peer react-dom@"15.x || ^16.0.0-0" from the root project npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.0.0" from @ant-design/cssinjs@1.24.0 npm warn node_modules/@ant-design/cssinjs npm warn @ant-design/cssinjs@"^1.18.4" from the root project npm warn 8 more (@umijs/plugins, antd, @ant-design/pro-card, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@"^18.3.1" from react-dom@18.3.1 npm warn node_modules/react-dom npm warn react-dom@"^18.2.0" from the root project npm warn 29 more (@antv/x6-react-shape, @umijs/preset-umi, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.0.0" from @ant-design/icons@5.6.1 npm warn node_modules/@ant-design/icons npm warn @ant-design/icons@"^5.2.6" from the root project npm warn 8 more (antd, @ant-design/pro-card, @ant-design/pro-field, ...) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/core@6.3.1 npm warn node_modules/@dnd-kit/core npm warn @dnd-kit/core@"^6.1.0" from the root project npm warn 3 more (@dnd-kit/modifiers, @dnd-kit/sortable, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react@18.3.1 npm warn node_modules/react npm warn react@"^18.2.0" from the root project npm warn 46 more (@antv/x6-react-shape, @dnd-kit/modifiers, ...) npm warn npm warn Could not resolve dependency: npm warn peer react@">=16.8.0" from @dnd-kit/utilities@3.2.2 npm warn node_modules/@dnd-kit/utilities npm warn @dnd-kit/utilities@"^3.2.2" from the root project npm warn 4 more (@dnd-kit/modifiers, @dnd-kit/sortable, @dnd-kit/core, @ant-design/pro-table) npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: peer react@"15.x || ^16.0.0-0" from the root project npm warn npm warn Could not resolve dependency: npm warn peer react@"15.x || ^16.0.0-0" from the root project npm warn ERESOLVE overriding peer dependency npm warn While resolving: dva@2.5.0-beta.2 npm warn Found: react-dom@undefined npm warn node_modules/react-dom npm warn peer react-dom@"15.x || ^16.0.0-0" from the root project npm warn npm warn Could not resolve dependency: npm warn peer react-dom@"15.x || ^16.0.0-0" from the root project ⠇
09-26
<think> 我们面对的核心问题是:项目使用React 18.3.1,但dva@2.5.0-beta.2要求React版本在15.x或16.x(即不兼容React 18)。同时,其他依赖(如@ant-design/cssinjs、@dnd-kit/core等)虽然表面上支持React 16+,但在React 18环境下与dva同时存在时,npm的peer依赖检查会发出警告。 ### 解决方案分析 #### 方案1:降级React到兼容版本(推荐稳定方案) **步骤:** 1. 将React和React-DOM降级到16.14.0(最后一个广泛兼容的16.x版本): ```bash npm install react@16.14.0 react-dom@16.14.0 --save-exact ``` 2. 清理并重新安装依赖: ```bash rm -rf node_modules package-lock.json npm install ``` *注意:使用`--save-exact`确保版本精确锁定。* **优点:** - 完全兼容dva@2.5.0-beta.2,消除所有peer依赖警告(包括dva、antd相关组件)[^1]。 **缺点:** - 无法使用React 18的新特性(如并发模式)。 #### 方案2:升级dva到支持React 18的版本(实验性方案) **步骤:** 1. 安装dva的React 18兼容分支(社区维护): ```bash npm install dva@2.6.0-beta.20 --save ``` 2. 检查其他依赖的兼容性: - 确保`@ant-design/cssinjs`升级到1.26.0+ - 确保`@dnd-kit`系列库升级到7.x **优点:** - 保留React 18的新特性。 **风险:** - dva@2.6.0-beta.20非官方稳定版本,可能存在未知问题。 #### 方案3:强制忽略依赖冲突(临时方案) 若上述方案均不可行,可临时使用npm的`--legacy-peer-deps`或`--force`: ```bash npm install --legacy-peer-deps ``` **作用:** - 跳过peer依赖检查,允许安装不一致的版本[^1]。 **警告:** - 可能导致运行时错误(如dva在React 18下失效)。 - 仅建议作为临时测试手段。 ### 分步操作指南(以方案1为例) #### 步骤1:修改package.json 确保dependencies字段中包含: ```json "react": "16.14.0", "react-dom": "16.14.0" ``` #### 步骤2:清理并重装依赖 ```bash rm -rf node_modules # 删除现有依赖 rm package-lock.json # 删除锁文件 npm install # 重新安装 ``` #### 步骤3:验证安装结果 ```bash npm ls react ``` 预期输出应显示: ``` react@16.14.0 ``` ### 兼容性修复验证 创建测试组件`src/test-dva.js`: ```jsx import React from 'react'; import { connect } from 'dva'; function TestDva({ dispatch }) { const handleClick = () => { dispatch({ type: 'test/load' }); }; return <button onClick={handleClick}>Test Dva</button>; } export default connect()(TestDva); ``` **预期行为:** - 页面渲染正常 - 点击按钮无错误(查看浏览器控制台) ### 常见问题处理 若降级后出现其他依赖警告(如antd): - 升级antd到最新兼容版本: ```bash npm install antd@4.24.0 --save # 确认兼容React 16的最高antd版本 ``` ### 最终建议 对于生产环境,**方案1(降级React)**是风险最低的选择;若项目必须使用React 18,则需评估**方案2(升级dva)**的稳定性。 > 引用说明:`npm install --legacy-peer-deps`方案来自npm官方文档对peer依赖冲突的临时处理建议[^1]。 §§相关问题§§ 1. 如何安全地进行React主版本降级? 2. npm 7+的peer dependency解析机制有哪些改进? 3. 大型项目中如何管理多级依赖版本冲突? 4. 有哪些工具可以可视化分析npm依赖树? </think> ### 解决 npm 依赖冲突的系统方案 #### 问题核心分析 项目中存在以下版本冲突: 1. **dva@2.5.0-beta.2** 要求 React 15.x 或 16.x(不兼容 React 18) 2. **当前 React 版本** 为 18.3.1 3. **其他依赖冲突**: - @ant-design/cssinjs (需要 React ≥16) - @dnd-kit/core (需要 React ≥16.8.0) - react-dom (需要精确匹配 React 18.3.1) #### 解决方案比较 | 方法 | 操作指令 | 优点 | 风险 | |-----------------------------|--------------------------------------|--------------------------|-------------------------------------| | **降级 React (推荐)** | `npm install react@16.14.0 react-dom@16.14.0 --save-exact` | 彻底解决dva兼容问题 | 放弃React 18新特性 | | **临时安装策略** | `npm install --legacy-peer-deps` | 快速绕过冲突 | 可能引入运行时错误 | | **强制安装** | `npm install --force` | 强制完成安装 | 依赖树可能不一致 | | **解析覆盖** | 在package.json添加: | 精确控制特定依赖版本 | 需手动维护所有冲突依赖 | ```json "resolutions": { "react": "16.14.0", "react-dom": "16.14.0" } ``` #### 最佳实践步骤(推荐降级方案) 1. **精确降级 React 版本** ```bash # 降级到dva兼容的最后一个稳定版 npm install react@16.14.0 react-dom@16.14.0 --save-exact ``` 2. **验证依赖树** ```bash npm ls react --depth=3 ``` 预期输出应包含: ``` react@16.14.0 ├── dva@2.5.0-beta.2 ├─┬ @ant-design/cssinjs@1.24.0 │ └── react@16.14.0 deduped ``` 3. **清理缓存并重装** ```bash rm -rf node_modules package-lock.json npm cache clean --force npm install ``` 4. **兼容性验证脚本** 创建 `src/dva-compat-test.jsx`: ```jsx import { connect } from 'dva'; export default connect()(() => { // 测试Hook兼容性(React 16.8+特性) const [state] = React.useState('Works!'); return <div>{`Dva+React 16兼容性测试: ${state}`}</div>; }); ``` #### 备选方案说明 若必须保留 React 18: 1. **使用 `--legacy-peer-deps`** ```bash # 临时解决方案(不推荐长期使用) npm install --legacy-peer-deps ``` **风险提示**:可能导致: - dva 生命周期方法失效 - React 18 并发特性与 dva 不兼容 - 热更新异常 2. **版本隔离方案** ```bash # 使用alias重定向依赖 npm install react-16@npm:react@16.14.0 npm install react-dom-16@npm:react-dom@16.14.0 ``` 在webpack配置中添加: ```js resolve: { alias: { 'react$': 'react-16', 'react-dom$': 'react-dom-16', 'dva': path.resolve(__dirname, 'node_modules/dva') } } ``` #### 验证指标 1. 成功标志: ```bash npm list | grep "react@" # 应显示16.14.0 npm run build # 无编译错误 ``` 2. 运行时检查: - dva 的 `app.model()` 正常注册 - React Hooks 在 dva 组件中正常执行 - 页面无 `Invalid hook call` 错误 > 根据 npm 官方文档,`--legacy-peer-deps` 是临时解决方案,长期项目建议使用精确版本降级[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值