遇到無法解析依賴樹的問題(依賴沖突)
npm i安裝依賴不成功,報如下錯誤:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: eslint-loader@1.9.0
npm ERR! Found: eslint@5.0.0
npm ERR! node_modules/eslint
npm ERR! dev eslint@"5.0.0" from the root project
npm ERR! peer eslint@">= 1.6.0 < 7.0.0" from @vue/cli-plugin-eslint@4.5.15
npm ERR! node_modules/@vue/cli-plugin-eslint
npm ERR! dev @vue/cli-plugin-eslint@"~4.5.0" from the root project
npm ERR! 9 more (eslint-loader, babel-eslint, eslint-config-standard, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer eslint@">=1.6.0 <5.0.0" from eslint-loader@1.9.0
npm ERR! node_modules/eslint-loader
npm ERR! dev eslint-loader@"1.9.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: eslint@4.19.1
npm ERR! node_modules/eslint
npm ERR! peer eslint@">=1.6.0 <5.0.0" from eslint-loader@1.9.0
npm ERR! node_modules/eslint-loader
npm ERR! dev eslint-loader@"1.9.0" from the root project
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\Administrator\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\Administrator\AppData\Local\npm-cache\_logs\2023-03-22T02_14_02_271Z-debug-0.log
解決方式
執(zhí)行 npm install --legacy-peer-deps
在package.json文件中,存在一個叫做peerDependencies(對等依賴關系)的對象,它包含了項目里需要的所有的包或則用戶正在下載的版本號相同的所有的包,意思就是對等依賴關系指定我們的包與某個特定版本的npm包兼容。對等依賴關系最好的例子就是React,一個聲明式的創(chuàng)建用戶界面的JS庫。
npm 從版本v7開始,install就默認以peerDependencies的方式去下載了。
策略如下:
如果用戶在根目錄的package.json文件里顯式依賴了核心庫,那么各個子項目里的peerDepenedencies聲明就可以忽略
如果用戶沒有顯式依賴核心庫,那么就按照子項目的peerDepenedencies中聲明的版本將依賴安裝到項目根目錄里
用戶依賴的包版本與各個子項目依賴的包版本相互不兼容,那么就會報錯(無法解析依賴樹的問題(依賴沖突))讓用戶自行去修復,因而導致安裝過程的中斷。(因為是從npm v7引入的,因此npm v3-v6就不會發(fā)生這個錯誤)
npm install --legacy-peer-deps命令用于繞過peerDependency里依賴的自動安裝;它告訴npm忽略項目中引入的各個依賴模塊之間依賴相同但版本不同的問題,以npm v3-v6的方式去繼續(xù)執(zhí)行安裝操作。
所以其實該命令并沒有真的解決沖突,而是忽略了沖突,以“過時”(v3-v6)的方式進行下載操作。文章來源:http://www.zghlxwxcb.cn/news/detail-656898.html
參考鏈接
https://blog.csdn.net/devcloud/article/details/124469666文章來源地址http://www.zghlxwxcb.cn/news/detail-656898.html
到了這里,關于安裝依賴不成功(npm install)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!