简体   繁体   English

为什么 ng new newApp 生成无法解决依赖树错误

[英]Why ng new newApp producing unable to resolve dependency tree error

I am trying to create a new angular app using the command ng new app-name .. But after running the command, it is showing the following in the command line.我正在尝试使用命令ng new app-name .. 创建一个新的 angular 应用程序。但运行该命令后,它在命令行中显示以下内容。

Installing packages (npm)...npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: angular-module@0.0.0
npm ERR! Found: jasmine-core@3.6.0
npm ERR! node_modules/jasmine-core
npm ERR!   dev jasmine-core@"~3.6.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer jasmine-core@">=3.7.1" from karma-jasmine-html-reporter@1.6.0
npm ERR! node_modules/karma-jasmine-html-reporter
npm ERR!   dev karma-jasmine-html-reporter@"^1.5.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 /home/vazha/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vazha/.npm/_logs/2021-05-07T05_12_55_121Z-debug.log
✖ Package install failed, see above.
The Schematic workflow failed. See above.

How can we fix this issue?我们如何解决这个问题?

This error is caused by an npm 7 issue.此错误是由 npm 7 问题引起的。 The Angular team recommends to use npm 6 for now. Angular 团队现在建议使用 npm 6。

Run npm install -g npm@6 to ensure that you are using that version.运行npm install -g npm@6以确保您使用的是该版本。

An alternative can be to run ng new with the --skipInstall flag, and then install the dependencies with npm install --legacy-peer-deps .另一种方法是使用--skipInstall标志运行ng new ,然后使用npm install --legacy-peer-deps依赖项。

First create the project as usual:首先像往常一样创建项目:

ng new <project>

Then go to the project directory and change version of jasmine-core in package.json to "^3.7.1".然后 go 到项目目录并将 package.json 中的 jasmine-core 版本更改为“^3.7.1”。 Then start install again:然后再次开始安装:

npm install

This is a temporary solution but it works这是一个临时解决方案,但它有效

Angular no longer recommends npm 6 (see npm install fails with NPM 7 for more information), so the current answer is a little different. Angular 不再推荐 npm 6(参见npm 安装失败,Z00A5CDC4BE82FD4BA549D52988EF9E1的答案略有不同)。

In this particular case you can try creating a new application and have the Angular CLI skip the npm install step.在这种特殊情况下,您可以尝试创建一个新应用程序并让 Angular CLI 跳过npm install步骤。 With Angular 12.x, the command is ng new your-project-name --skip-install .对于 Angular 12.x,命令是ng new your-project-name --skip-install This will still setup Git, and just require a npm install .这仍然会设置 Git,并且只需要npm install

Before doing so, however, you'll want to determine what the issue is and fix the package.json.但是,在这样做之前,您需要确定问题所在并修复 package.json。

This particular dependency seems to be recurring issue, as it started breaking again.这种特殊的依赖性似乎是反复出现的问题,因为它又开始崩溃了。 See npm ERR: Could not resolve dependency.请参阅npm ERR:无法解决依赖关系。 npm ERR. npm 错误。 peer jasmine-core@">=3.8" from karma-jasmine-html-reporter@1.7.0 . peer jasmine-core@">=3.8" 来自 karma-jasmine-html-reporter@1.7.0

For individuals coming in early July 2021, update package.json so that "jasmine-core": "~3.8.0", is listed in the package.json, and then run npm install in the root of the project directory. For individuals coming in early July 2021, update package.json so that "jasmine-core": "~3.8.0", is listed in the package.json, and then run npm install in the root of the project directory. This should leave you ready to go, which you should be able to confirm with a ng serve --open .这应该让您准备好 go,您应该可以使用ng serve --open来确认。

The full steps:完整步骤:

  1. ng new your-project-name --skip-install
  2. cd your-project-name
  3. Open package.json in the editor of your choice.在您选择的编辑器中打开 package.json。
  4. Edit the package.json to fix the dependency issue.编辑 package.json 以修复依赖性问题。
  5. npm install
  6. Fix any issues that may arise, if needed.如果需要,修复可能出现的任何问题。
  7. ng serve --open to verify the project is up and running. ng serve --open以验证项目是否已启动并正在运行。

If you're still experiencing this after trying all of the above options, just downgrade npm version from 7 to 6: npm install -g npm@6如果您在尝试上述所有选项后仍然遇到此问题,只需将 npm 版本从 7 降级到 6: npm install -g npm@6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM