項目場景:
使用npm或yarn安裝第三方包。
問題描述
使用npm命令和yarn命令混合使用會出現(xiàn)安裝包沖突。
E:\WorkSpace\react\zhuangao_05\dome_01>npm i echarts echarts-for-react
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @jiaminghi/data-view-react@1.2.5
npm ERR! Found: react@18.2.0
npm ERR! node_modules/react
npm ERR! peer react@">=16.0.0" from @ant-design/cssinjs@1.3.2
npm ERR! node_modules/@ant-design/cssinjs
npm ERR! @ant-design/cssinjs@"^1.3.0" from antd@5.1.2
npm ERR! node_modules/antd
npm ERR! antd@"^5.0.4" from the root project
npm ERR! peer react@">=16.0.0" from @ant-design/icons@4.8.0
npm ERR! node_modules/@ant-design/icons
npm ERR! @ant-design/icons@"^4.7.0" from antd@5.1.2
npm ERR! node_modules/antd
npm ERR! antd@"^5.0.4" from the root project
npm ERR! @ant-design/icons@"^4.8.0" from the root project
npm ERR! 53 more (@ant-design/react-slick, @rc-component/portal, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0" from @jiaminghi/data-view-react@1.2.5
npm ERR! node_modules/@jiaminghi/data-view-react
npm ERR! @jiaminghi/data-view-react@"^1.2.5" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: react@16.14.0
npm ERR! node_modules/react
npm ERR! peer react@"^16.8.0" from @jiaminghi/data-view-react@1.2.5
npm ERR! node_modules/@jiaminghi/data-view-react
npm ERR! @jiaminghi/data-view-react@"^1.2.5" 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\86158\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\86158\AppData\Local\npm-cache\_logs\2023-01-09T06_13_18_485Z-debug-0.log
原因分析:
如果將
yarn.lock
和package.lock
文件放在一起,有時會變得很困難,因為存在不同步的風(fēng)險yarn install
安裝生成的yarn.lock
文件 與npm install
生成的package-lock.json
文件時間相差較遠,造成了yarn.lock
的包版本低于package-lock.json
的包版本。因為以 ?x.x.x 形式定義的包版本在不同時期安裝包版本不一致。yarn
安裝 @babel/preset-env 版本有: “7.5.5”, “^7.4.5”,實際安裝的 version 是 “7.5.5”。npm
與yarn
安裝包不一致
紗線和npm是可以互換的。只要你每次都使用相同的一個,它們之間就沒有區(qū)別。它們有不同的安裝目錄,這就是它們不能一起使用的原因。Yarn會安裝一個包,npm找不到它。npm會安裝一個包,yarn找不到它
解決方案:
1、npm i xxx --legacy-peer-deps
使用
npm i xxx --legacy-peer-deps
,命令來安裝依賴!
為什么?作用?
npm install xxxx --legacy-peer-deps命令與其說是告訴npm要去干什么,不如說是告訴npm不要去干什么。
legacy的意思:遺產(chǎn)/(軟件或硬件)已過時但因使用范圍廣而難以替代的;而npm install xxxx --legacy-peer-deps命令用于繞過peerDependency里依賴的自動安裝;它告訴npm忽略項目中引入的各個依賴模塊之間依賴相同但版本不同的問題,以npm v3-v6的方式去繼續(xù)執(zhí)行安裝操作。
所以其實該命令并沒有真的解決沖突,而是忽略了沖突,以“過時”(v3-v6)的方式進行下載操作。
2、刪除所有關(guān)于npm安裝依賴的文件
刪除之后再重新npm i ,安裝需要的第三方包文章來源:http://www.zghlxwxcb.cn/news/detail-492251.html
為了防止沖突文件,所以使用一致的命令來安裝第三方庫。文章來源地址http://www.zghlxwxcb.cn/news/detail-492251.html
到了這里,關(guān)于npm i時報錯npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!