解决 NPM 安装应用出现 ERESOLVE 错误

Alan

Alan

Maintainer of blog

今天在项目里执行 npm i 出现以下错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: project-name@0.0.1
npm ERR! Found: typescript@3.6.3
npm ERR! node_modules/typescript
npm ERR! dev typescript@"3.6.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer typescript@"^3.6.4" from @nestjs/cli@6.12.9
npm ERR! node_modules/@nestjs/cli
npm ERR! dev @nestjs/cli@"6.12.9" 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 /root/.npm/eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-03-30T14_18_07_395Z-debug.log

网上搜了一下,是因为NPM从版本7开始在某些方面比之前版本更加严格,改成 npm i --legacy-peer-deps 后解决。

以下是原文:

ERESOLVE issues with npm@7 are common because npm 7.x is more strict about certain things than npm 6.x. Often, the easiest resolution is to pass the --legacy-peer-deps flag to npm (e.g., npm i --legacy-peer-deps) or else use npm@6.

If that doesn't work immediately, perhaps try removing node_modules and package-lock.json first. They will be recreated.

Tip

You don't need to uninstall npm@7 to use npm@6. Use npx to specify the version of npm instead. For example: npx -p npm@6 npm i --legacy-peer-deps.