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

qyzbxkaa  于 12个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(173)

我想将Mock Service Worker包含到我的React项目中,并提供Typescript支持,但当我运行npm install msw --save-dev命令时,我的终端显示以下错误消息:

PS F:\Programming\React Practices\ReactPizza\my-app> npm install msw --save-dev
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/typescript
npm ERR!   peer typescript@">= 2.7" from [email protected]
npm ERR!   node_modules/fork-ts-checker-webpack-plugin
npm ERR!     fork-ts-checker-webpack-plugin@"^6.5.0" from [email protected]
npm ERR!     node_modules/react-dev-utils
npm ERR!       react-dev-utils@"^12.0.1" from [email protected]
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 [email protected]
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 [email protected]
npm ERR! node_modules/msw
npm ERR!   dev msw@"*" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/typescript
npm ERR!   peerOptional typescript@">= 4.2.x <= 4.6.x" from [email protected]
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:\Users\Win10\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\Win10\AppData\Local\npm-cache\_logs\2022-05-27T17_17_28_323Z-debug.log

字符串
我不知道是什么问题,以及如何解决它。我的迷你项目是由React-app驱动的。我的意思是,该项目是这样开始的:

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


当我不使用Typescript与npx react-app时,一切都很顺利。
您可以通过尝试安装MSW在GitHub:https://github.com/AlexKor-5/ReactPizzaApp_Ts/tree/5849bb4cb928550dcb4bfd66ecb8b7bf0757fb9f上轻松检查这些错误消息。

lymnna71

lymnna711#

我只能假设你使用的是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,而您安装了[[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection),这是因为msw不接受您的typescript版本。

7vux5j2d

7vux5j2d2#

如果您正在升级msw,请参阅:https://mswjs.io/docs/migrations/1.x-to-2.x/?secureweb=Teams

// jest.config.js
module.exports = {
  testEnvironmentOptions: {
    customExportConditions: [''],
  },
}

字符串

相关问题