React+TypeScript项目无法安装依赖:依赖冲突问题
React+TypeScript项目依赖安装时的TypeScript版本冲突问题
在我的React+TypeScript项目中,尝试安装任何依赖都会出现TypeScript版本冲突问题。已尝试更新依赖、降级依赖、删除node_modules和package-lock.json后重新安装、清理npm缓存,但均无法解决问题。
复现步骤
npm install axios
错误信息
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: react-scripts@5.0.1 npm ERR! Found: typescript@5.2.2 npm ERR! node_modules/typescript npm ERR! peer typescript@"*" from eslint-config-standard-with-typescript@34.0.1 npm ERR! node_modules/eslint-config-standard-with-typescript npm ERR! dev eslint-config-standard-with-typescript@"^34.0.1" from the root project npm ERR! peer typescript@">= 2.7" from fork-ts-checker-webpack-plugin@6.5.3 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! 2 more (tsutils, the root project) npm ERR! npm ERR! Could not resolve dependency: 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! npm ERR! Conflicting peer dependency: typescript@4.9.5 npm ERR! node_modules/typescript 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! 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.
项目package.json内容
{ "name": "front", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "@types/jest": "^29.5.5", "@types/node": "^20.8.2", "@types/react": "^18.2.24", "@types/react-dom": "^18.2.9", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.16.0", "react-scripts": "^5.0.1", "web-vitals": "^2.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.55.0", "@typescript-eslint/parser": "^5.55.0", "eslint": "^8.36.0", "eslint-config-standard-with-typescript": "^34.0.1", "eslint-plugin-import": "^2.27.5", "eslint-plugin-n": "^15.6.1", "eslint-plugin-promise": "^6.1.1", "eslint-plugin-react": "^7.32.2", "typescript": "^5.2.2" } }
已尝试的修复方法
- 更新TypeScript(初始版本为5.0.1):
npm install typescript@5.2.2 - 删除node_modules和package-lock.json:
rm -rf node_modules rm -rf package-lock.json - 清理npm缓存:
npm cache clean -f
内容的提问来源于stack exchange,提问作者Rafael Ribeiro
相关产品推荐
相关产品推荐

