为什么没有在React项目中安装支持Typescript的Mock Service Worker



我想在支持Typescript的React项目中包含Mock Service Worker,但一旦我运行npm install msw--save dev命令,我的终端就会显示以下错误消息:

PS F:ProgrammingReact PracticesReactPizzamy-app> npm install msw --save-dev
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: msw@0.41.0
npm ERR! Found: typescript@4.7.2
npm ERR! node_modules/typescript
npm ERR!   peer typescript@">= 2.7" from fork-ts-checker-webpack-plugin@6.5.2
npm ERR!   node_modules/fork-ts-checker-webpack-plugin
npm ERR!     fork-ts-checker-webpack-plugin@"^6.5.0" from react-dev-utils@12.0.1
npm ERR!     node_modules/react-dev-utils
npm ERR!       react-dev-utils@"^12.0.1" from react-scripts@5.0.1
npm ERR!       node_modules/react-scripts
npm ERR!         react-scripts@"5.0.1" from the root project
npm ERR!   peerOptional typescript@"^3.2.1 || ^4" from react-scripts@5.0.1
npm ERR!   node_modules/react-scripts
npm ERR!     react-scripts@"5.0.1" from the root project
npm ERR!   2 more (tsutils, the root project)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peerOptional typescript@">= 4.2.x <= 4.6.x" from msw@0.41.0
npm ERR! node_modules/msw
npm ERR!   dev msw@"*" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: typescript@4.6.4
npm ERR! node_modules/typescript
npm ERR!   peerOptional typescript@">= 4.2.x <= 4.6.x" from msw@0.41.0
npm ERR!   node_modules/msw
npm ERR!     dev msw@"*" 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:UsersWin10AppDataLocalnpm-cacheeresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR!     C:UsersWin10AppDataLocalnpm-cache_logs2022-05-27T17_17_28_323Z-debug.log

我不知道这是怎么回事,也不知道怎么解决。我的迷你项目由create-react应用程序提供支持。我的意思是这个项目是这样开始的:

npx create-react-app my-app --template typescript

当我不使用npx创建反应应用程序的Typescript时,一切都很顺利。

你可以很容易地在GitHub上查看这些错误消息:https://github.com/AlexKor-5/ReactPizzaApp_Ts/tree/5849bb4cb928550dcb4bfd66ecb8b7bf0757fb9f通过尝试安装MSW。

我只能假设您使用的是npm 7(因为npm 6会自动强制安装它)。它告诉你问题是什么,一个冲突的对等依赖typescript

它还告诉你如何修复它:

Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps

阿卡:

npm install --legacy-peer-deps

否则,请重新安装任一软件包的更新/早期版本,以便对等依赖项能够匹配。Msw正在使用">= 4.2.x <= 4.6.x"类型脚本,而您已经安装了typescript@4.7.2,这很糟糕,因为Msw不接受您的类型脚本版本。

最新更新