创建新的 Angular 项目时出现 npm 依赖错误

时间:2021-02-03 14:50:26

标签: angular-cli

今天我决定进入框架并选择 Angular 作为开始。我正在使用 Node 和 npm,我以前从未使用过它们。我不是编程本身的菜鸟,但这对我来说是新的。

现在,当我尝试创建一个名为“banking-app”的新 Angular 项目时,我在节点中得到以下输出:

ng new banking-app
? Do you want to enforce stricter type checking and stricter bundle budgets in the workspace?
  This setting helps improve maintainability and catch bugs ahead of time.
  For more information, see https://angular.io/strict No
? Would you like to add Angular routing? No
? Which stylesheet format would you like to use? CSS
CREATE banking-app/angular.json (3575 bytes)
CREATE banking-app/package.json (1201 bytes)
CREATE banking-app/README.md (1019 bytes)
CREATE banking-app/tsconfig.json (538 bytes)
CREATE banking-app/tslint.json (3185 bytes)
CREATE banking-app/.editorconfig (274 bytes)
CREATE banking-app/.gitignore (631 bytes)
CREATE banking-app/.browserslistrc (703 bytes)
CREATE banking-app/karma.conf.js (1428 bytes)
CREATE banking-app/tsconfig.app.json (287 bytes)
CREATE banking-app/tsconfig.spec.json (333 bytes)
CREATE banking-app/src/favicon.ico (948 bytes)
CREATE banking-app/src/index.html (296 bytes)
CREATE banking-app/src/main.ts (372 bytes)
CREATE banking-app/src/polyfills.ts (2830 bytes)
CREATE banking-app/src/styles.css (80 bytes)
CREATE banking-app/src/test.ts (753 bytes)
CREATE banking-app/src/assets/.gitkeep (0 bytes)
CREATE banking-app/src/environments/environment.prod.ts (51 bytes)
CREATE banking-app/src/environments/environment.ts (662 bytes)
CREATE banking-app/src/app/app.module.ts (314 bytes)
CREATE banking-app/src/app/app.component.html (25725 bytes)
CREATE banking-app/src/app/app.component.spec.ts (955 bytes)
CREATE banking-app/src/app/app.component.ts (215 bytes)
CREATE banking-app/src/app/app.component.css (0 bytes)
CREATE banking-app/e2e/protractor.conf.js (904 bytes)
CREATE banking-app/e2e/tsconfig.json (274 bytes)
CREATE banking-app/e2e/src/app.e2e-spec.ts (662 bytes)
CREATE banking-app/e2e/src/app.po.ts (274 bytes)
- Installing packages (npm)...npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: banking-app@0.0.0
npm ERR! Found: typescript@4.1.3
npm ERR! node_modules/typescript
npm ERR!   dev typescript@"~4.1.2" from the root project
npm ERR!   peer typescript@"~4.0.0 || ~4.1.0" from @angular-devkit/build-angular@0.1101.2
npm ERR!   node_modules/@angular-devkit/build-angular
npm ERR!     dev @angular-devkit/build-angular@"~0.1101.2" from the root project
npm ERR!   2 more (@angular/compiler-cli, ng-packagr)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer typescript@"~3.9.5" from tsickle@0.39.1
npm ERR! node_modules/tsickle
npm ERR!   peerOptional tsickle@"~0.39.0" from ng-packagr@11.1.3
npm ERR!   node_modules/ng-packagr
npm ERR!     peerOptional ng-packagr@"^11.0.0 || ^11.1.0-next" from @angular-devkit/build-angular@0.1101.2
npm ERR!     node_modules/@angular-devkit/build-angular
npm ERR!       dev @angular-devkit/build-angular@"~0.1101.2" 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\xyz\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\xyz\AppData\Local\npm-cache\_logs\2021-02-03T14_27_21_486Z-debug.log
× Package install failed, see above.
The Schematic workflow failed. See above.

我对什么是依赖错误有一个大致的了解,但在这种情况下如何管理它却是零想法。我对 node、npm 等很陌生,所以我真的可以使用一些帮助来开始这个:|

提前致谢!

4 个答案:

答案 0 :(得分:1)

此错误出现在 7 版本的 npm(开发版)中。尝试使用 npm 的最后一个 LTS 版本,现在是 v6.14.11。要安装它,请在终端 npm i npm@v6.14.11 -g

中运行命令

答案 1 :(得分:0)

所以我遇到了同样的问题,我尝试了@Miruk 所说的(即更新您的 angular cli),但没有奏效。因此,如果您遇到这种情况并且更新不起作用,请尝试运行:

<块引用>

npm set config legacy-peer-deps true

这最终对我有用。祝你好运!

答案 2 :(得分:0)

我也遇到了这个问题,通过安装节点的 lts 版本而不是最新版本来解决。我认为某些依赖项尚未更新以适用于最新版本。

答案 3 :(得分:0)

我的新 angular 项目遇到了同样的错误,这解决了:

我将 Node js 从 v.12 升级到 v.14,然后 npm install npm@latest -g

此外,如果您使用的是 angular,请尝试在项目中执行 ng new <name> --skip-install,然后执行 npm i