?起因:由于使用"react": "^19.0.0",? 第三方包要低版本react,錯解決方法!
npm install --save emoji-mart @emoji-mart/data @emoji-mart/react
npm install --save emoji-mart @emoji-mart/data @emoji-mart/react
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: synergy-web@0.0.0
npm error Found: react@19.1.0
npm error node_modules/react
npm error react@"^19.0.0" from the root project
npm error
npm error Could not resolve dependency:
npm error peer react@"^16.8 || ^17 || ^18" from @emoji-mart/react@1.1.1
npm error node_modules/@emoji-mart/react
npm error @emoji-mart/react@"*" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.
npm error
npm error
npm error For a full report see:
npm error C:\Users\AreYouOK\AppData\Local\npm-cache\_logs\2025-04-16T02_36_44_692Z-eresolve-report.txt
npm error A complete log of this run can be found in: C:\Users\BIM\AppData\Local\npm-cache\_logs\2025-04-16T02_36_44_692Z-debug-0.log
推薦方式:使用 --legacy-peer-deps
這個命令會跳過嚴格的依賴樹校驗,允許你安裝不完全兼容的依賴。大多數情況下,如果 @emoji-mart/react
沒有用到 React 19 的 breaking change,是可以正常運行的。
npm install --save emoji-mart @emoji-mart/data @emoji-mart/react --legacy-peer-deps
added 3 packages in 6s
81 packages are looking for funding
🚨 備選方式(不太推薦):使用 --force
這個命令會強制安裝,不管依賴沖突,可能會帶來潛在風險。
AI提示詞:?--legacy-peer-deps 參數意義
AI回復:
🧠 --legacy-peer-deps
是什么?
這個參數的作用是:
跳過對 peerDependencies 的嚴格校驗,使用舊版 npm(v6 及之前)的處理方式來安裝依賴。
🔍 背景知識:什么是 peerDependencies?
-
peerDependencies
表示某個包期望你項目中安裝了一個特定版本的依賴,但它自己不會安裝。 -
比如
@emoji-mart/react
聲明:
?"peerDependencies": {"react": "^16.8 || ^17 || ^18" }
它的意思是:我不會裝 React,但你必須在項目里裝我兼容的 React 版本。
?? 默認情況下(npm 7+):
-
如果你項目里用了 不匹配的依賴版本(比如 React 19),npm 會報錯并中止安裝。
-
這就是你看到的
ERESOLVE unable to resolve dependency tree
。
? 使用 --legacy-peer-deps
會怎么樣?
-
npm 會像 v6 那樣 不管這些版本不一致的問題,繼續把包安裝下來。
-
它相信你知道你在做什么。
?? 簡單對比總結:
行為 | 默認安裝(npm 7+) | --legacy-peer-deps |
---|---|---|
檢查 peerDependencies | ? 嚴格檢查 | ? 跳過檢查 |
依賴版本沖突時 | ? 安裝失敗 | ? 強行安裝 |
是否安全 | ? 更安全 | ?? 可能會出問題 |
AI提示詞: @emoji-mart/data @emoji-mart/react 這兩個庫 19能用嗎?
AI回復:
好問題!來,我們逐個分析下 @emoji-mart/react
和 @emoji-mart/data
在 React 19 下的兼容情況 ?
1. @emoji-mart/react
🔧 當前版本:
-
最新穩定版:
v1.1.1
(截至 2025 年 4 月) -
它的
peerDependencies
限制為:react: ^16.8 || ^17 || ^18
? 結論(目前):
-
npm 會報錯,因為 React 19 超出了它聲明的兼容范圍。
-
即便你用
--legacy-peer-deps
裝上了,不代表 100% 兼容,但很多人測試后發現其實能正常跑 —— 只是它還沒“官方”支持 React 19。
? 臨時方案:
你可以手動裝它 + legacy 參數來跑試試:
npm install @emoji-mart/react @emoji-mart/data --legacy-peer-deps或者加進 package.json 后直接用 npm install --legacy-peer-deps
📦 2. @emoji-mart/data
這個包只是 emoji 數據(JSON 文件),跟 React 無關,可以放心使用在任何 React 版本里(包括 React 19)。
? 完全兼容 React 19,無副作用
🎯 總結:
包名 | React 19 狀態 | 安裝建議 |
---|---|---|
@emoji-mart/react | ? 不聲明支持,但可勉強運行 | --legacy-peer-deps 安裝 |
@emoji-mart/data | ? 安全兼容 | 正常安裝即可 |