You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

下载Angular项目后npm安装依赖报错,是否与版本相关?

问题描述

我希望下载Angular项目源码并在本地浏览器中运行应用,项目是一个Angular上传Excel/PDF的示例。我将项目下载到本地桌面后,执行了以下命令:

npm install node--modules --save

但出现了如下错误:

npm install node--modules --save
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: angular@0.0.0
npm ERR! Found: @angular/compiler@7.2.11
npm ERR! node_modules/@angular/compiler
npm ERR!   @angular/compiler@"7.2.11" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/compiler@"7.0.4" from @angular/compiler-cli@7.0.4
npm ERR! node_modules/@angular/compiler-cli
npm ERR!   dev @angular/compiler-cli@"~7.0.0" 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 C:\Users\test\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\test\AppData\Local\npm-cache\_logs\2022-09-26T02_53_24_422Z-debug-0.log

请问这是否与Angular版本有关?

解决方案

1. 修正错误的安装命令

你执行的npm install node--modules --save是错误的:node_modules是npm自动生成的依赖目录,不需要作为包名进行安装。正确的安装命令是:

npm install

2. 错误核心原因:Angular版本依赖冲突

是的,这个错误确实和Angular版本有关。项目中@angular/compiler的版本是7.2.11,但开发依赖@angular/compiler-cli的版本是~7.0.0,它要求的peer依赖是@angular/compiler@7.0.4,两者版本不匹配,导致npm(尤其是npm v7及以上版本,默认严格检查peer依赖)无法解析依赖树。

3. 具体修复步骤

  • 清理现有错误依赖:删除本地的node_modules目录和package-lock.json文件,避免残留错误依赖。
  • 执行兼容安装:因为这是Angular 7的老项目,建议使用--legacy-peer-deps参数跳过严格的peer依赖检查,执行:
    npm install --legacy-peer-deps
    
  • 彻底解决版本冲突(可选):修改项目根目录的package.json文件,将devDependencies中的@angular/compiler-cli版本从~7.0.0改为~7.2.11,和@angular/compiler版本保持一致,然后再执行npm install。

4. 运行项目

依赖安装完成后,执行以下命令启动本地开发服务器:

ng serve

启动成功后,在浏览器中访问http://localhost:4200即可运行应用。


内容的提问来源于stack exchange,提问作者Fanny Zhingre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:20:36