無法安裝React應用程式依賴項,因為node_modules資料夾不見了,並且會引發錯誤
P粉616111038
2023-09-04 17:12:27
<p>我正在嘗試安裝React應用,但無法安裝,它總是給出以下錯誤並且整個node_modules資料夾消失了。 </p>
<pre class="brush:php;toolbar:false;">npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Found: 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! Could not resolve dependency:
npm ERR! peer react-scripts@"^4.0.0" from @craco/craco@6.4.5
npm ERR! node_modules/@craco/craco
npm ERR! @craco/craco@"^6.4.3" 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 /Users/xoxo/.npm/eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/xoxo/.npm/_logs/2023-01-24T15_48_03_164Z-debug-0.log</pre>
<p>而且它也不讓我安裝<code>react-app-rewired</code></p>
<p>我嘗試了使用--force和--legacy-peer-deps,但沒有運氣,我已經卡在這裡3個小時了,無法從任何地方解決它。也嘗試了清除快取並重新安裝的方法,但也沒有運氣。 </p>
<p>對於某些私有包,我使用了<code>npm auth</code>和<code>$NPM_TOKEN</code>,但仍然出現以下錯誤:</p>
<pre class="brush:php;toolbar:false;">npm auth
npm ERR! code EUSAGE
npm ERR!
npm ERR! Manage package owners
npm ERR!
npm ERR! Usage:
npm ERR! npm owner add <user> <package-spec>
npm ERR! npm owner rm <user> <package-spec>
npm ERR! npm owner ls <package-spec>
npm ERR!
npm ERR! Options:
npm ERR! [--registry <registry>] [--otp <otp>]
npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm ERR! [-ws|--workspaces]
npm ERR!
npm ERR! alias: author
npm ERR!
npm ERR! Run "npm help owner" for more info
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/xoxo/.npm/_logs/2023-01-26T07_54_36_914Z-debug-0.log</pre></p>
用以下方法解決了上述問題:
npm i
失敗後,使用了npm i --legacy-peer-deps
,之前先執行了npm cache clean --force
#哇!所有的依賴都被安裝了
我最初犯的錯誤是嘗試使用npm auth,但實際上並不需要,因為
npm
會從.envrc
和.npmrc
檔案中取得它因此,如果你遇到類似的困難,請嘗試使用這些步驟