報(bào)錯(cuò)處理node While resolving: awesome-typescript-loader@5.2.1 Found: typescript@4.9.5 node_modules/ty...

解決 npm 依賴(lài)沖突報(bào)錯(cuò)記錄

最近在使用 npm 安裝項(xiàng)目依賴(lài)時(shí),遇到了一個(gè)關(guān)于依賴(lài)沖突的問(wèn)題,特此記錄一下解決過(guò)程。

報(bào)錯(cuò)內(nèi)容如下

# npm resolution error report

While resolving: awesome-typescript-loader@5.2.1
Found: typescript@4.9.5
node_modules/typescript
  dev typescript@"^4.4.4" from the root project
  peer typescript@"*" from ts-loader@9.4.4
  node_modules/ts-loader
    dev ts-loader@"^9.2.6" from the root project
  peer typescript@">=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta" from tsutils@3.21.0
  node_modules/tsutils
    tsutils@"^3.21.0" from @typescript-eslint/eslint-plugin@5.62.0
    node_modules/@typescript-eslint/eslint-plugin
      dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project
    tsutils@"^3.21.0" from @typescript-eslint/type-utils@5.62.0
    node_modules/@typescript-eslint/type-utils
      @typescript-eslint/type-utils@"5.62.0" from @typescript-eslint/eslint-plugin@5.62.0
      node_modules/@typescript-eslint/eslint-plugin
        dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project
    tsutils@"^3.21.0" from @typescript-eslint/typescript-estree@5.62.0
    node_modules/@typescript-eslint/typescript-estree
      @typescript-eslint/typescript-estree@"5.62.0" from @typescript-eslint/parser@5.62.0
      node_modules/@typescript-eslint/parser
        dev @typescript-eslint/parser@"^5.3.1" from the root project
        peer @typescript-eslint/parser@"^5.0.0" from @typescript-eslint/eslint-plugin@5.62.0
        node_modules/@typescript-eslint/eslint-plugin
          dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project
      @typescript-eslint/typescript-estree@"5.62.0" from @typescript-eslint/type-utils@5.62.0
      node_modules/@typescript-eslint/type-utils
        @typescript-eslint/type-utils@"5.62.0" from @typescript-eslint/eslint-plugin@5.62.0
        node_modules/@typescript-eslint/eslint-plugin
          dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project
      @typescript-eslint/typescript-estree@"5.62.0" from @typescript-eslint/utils@5.62.0
      node_modules/@typescript-eslint/utils
        @typescript-eslint/utils@"5.62.0" from @typescript-eslint/eslint-plugin@5.62.0
        node_modules/@typescript-eslint/eslint-plugin
          dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project
        @typescript-eslint/utils@"5.62.0" from @typescript-eslint/type-utils@5.62.0
        node_modules/@typescript-eslint/type-utils
          @typescript-eslint/type-utils@"5.62.0" from @typescript-eslint/eslint-plugin@5.62.0
          node_modules/@typescript-eslint/eslint-plugin
            dev @typescript-eslint/eslint-plugin@"^5.3.0" from the root project

Could not resolve dependency:
peer typescript@"^2.7 || ^3" from awesome-typescript-loader@5.2.1
node_modules/awesome-typescript-loader
  dev awesome-typescript-loader@"^5.2.1" from the root project

Conflicting peer dependency: typescript@3.9.10
node_modules/typescript
  peer typescript@"^2.7 || ^3" from awesome-typescript-loader@5.2.1
  node_modules/awesome-typescript-loader
    dev awesome-typescript-loader@"^5.2.1" from the root project

Fix the upstream dependency conflict, or retry
this command with --force or --legacy-peer-deps
to accept an incorrect (and potentially broken) dependency resolution.

問(wèn)題描述

在解析 awesome-typescript-loader@5.2.1 依賴(lài)時(shí),發(fā)現(xiàn)了以下依賴(lài)關(guān)系:

  • 發(fā)現(xiàn) typescript@4.9.5,位于 node_modules/typescript
    • 來(lái)自根項(xiàng)目的 dev typescript@"^4.4.4"
    • 來(lái)自 ts-loader@9.4.4peer typescript@"*"
    • 來(lái)自根項(xiàng)目的 dev ts-loader@"^9.2.6"
    • 來(lái)自 tsutils@3.21.0peer typescript@">=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta"
  • 發(fā)現(xiàn) typescript@3.9.10,位于 node_modules/typescript
    • 來(lái)自 awesome-typescript-loader@5.2.1peer typescript@"^2.7 || ^3"

由于這兩個(gè)版本的 typescript 存在沖突,導(dǎo)致依賴(lài)解析失敗。

問(wèn)題分析

通過(guò)上面的錯(cuò)信息我們已經(jīng)了解到可以使用 --force--legacy-peer-deps 選項(xiàng)來(lái)嘗試重新解析依賴(lài)。這樣可能會(huì)接受不正確的依賴(lài)解析,但也可能導(dǎo)致破損的依賴(lài)。
另一種解決方法是嘗試修復(fù)上游的依賴(lài)沖突。這可能需要手動(dòng)調(diào)整項(xiàng)目的依賴(lài)配置,以確保所有依賴(lài)的版本能夠協(xié)調(diào)工作。
根據(jù)日志,問(wèn)題是 awesome-typescript-loader 的 peerDependencies 中指定了對(duì) typescript 的版本依賴(lài)是 "^2.7 || ^3",但項(xiàng)目中實(shí)際安裝的 typescript 版本是 4.9.5,高于指定的范圍,因此產(chǎn)生沖突。

解決方案

有幾種解決方法:

  1. 升級(jí) awesome-typescript-loader 到最新版本,它移除了對(duì) typescript 版本的硬性限制,可以兼容 typescript 4.x
npm install awesome-typescript-loader@latest
  1. 降級(jí)項(xiàng)目中 typescript 版本到 3.x 來(lái)滿足 awesome-typescript-loader 的 peerDependencies
npm install typescript@3 --save-dev
  1. 使用 --legacy-peer-deps 參數(shù)強(qiáng)制安裝,忽略 peerDependencies 不兼容問(wèn)題
npm install awesome-typescript-loader --legacy-peer-deps
  1. 如果是通過(guò) yarn 安裝,可以使用 yarn resolution 強(qiáng)制使用 typescript 4.x
yarn resolution:use-native typescript@4.x  

建議使用方法 1,升級(jí)到最新版本的 awesome-typescript-loader。?

方法 2 也可以,但會(huì)失去 typescript 4.x 的一些新特性。?

方法 3 可以解決安裝問(wèn)題,但可能會(huì)引發(fā)實(shí)際運(yùn)行時(shí)的不兼容問(wèn)題。???

所以綜合來(lái)說(shuō),升級(jí) awesome-typescript-loader 是更好的長(zhǎng)期解決方案。

結(jié)論

依賴(lài)沖突是開(kāi)發(fā)過(guò)程中常見(jiàn)的問(wèn)題,解決起來(lái)可能需要一些時(shí)間和耐心。在解決依賴(lài)沖突問(wèn)題時(shí),需要注意官方文檔、社區(qū)討論以及類(lèi)似問(wèn)題的解決方法,以便找到最合適的解決方案。

希望這篇記錄能夠?qū)ζ渌_(kāi)發(fā)者在遇到類(lèi)似問(wèn)題時(shí)提供一些幫助和指導(dǎo)。如果你也遇到了類(lèi)似的問(wèn)題,歡迎分享你的解決方法和經(jīng)驗(yàn)!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,401評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,011評(píng)論 3 413
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 175,263評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 62,543評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,323評(píng)論 6 404
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 54,874評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,968評(píng)論 3 439
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,095評(píng)論 0 286
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,605評(píng)論 1 331
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,551評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,720評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,242評(píng)論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,961評(píng)論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,358評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,612評(píng)論 1 280
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,330評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,690評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容