当尝试使用npm i命令安装npm包时,我得到了以下异常:

我尝试重新安装Node.js包,并使用以下方法将代理设置为关闭:

set HTTP_PROXY=
set HTTPS_PROXY=

问题仍然存在。我哪里做错了?

更新:

执行以下命令时:

npm install --legacy-peer-deps

系统显示如下错误:


当前回答

首先要了解问题。以下是我的错误:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: project-admin@11.0.0
npm ERR! Found: @angular/common@11.0.3
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"11.0.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^9.1.0 || ^10.0.0" from @agm/core@3.0.0-beta.0
npm ERR! node_modules/@agm/core
npm ERR!   @agm/core@"3.0.0-beta.0" from the root project

首先,你应该从下到上读这道题。这里@agm/core@3.0.0-beta.0需要通用的angular 9.1.0或10.0.0。最上面的消息说,角的公共发现实际上是11.0.3。

(如果你想更好地理解依赖关系,这里有一个非常简单的网站:如何npm3工作)

dependencies — these are the essential dependencies that you rely on and call in your project’s code
devDependencies — these are your development dependencies, for example, a prettier library for formatting code
peerDependencies — if you set a peer dependency in your package.json, you are telling the person who installs your package that they need that dependency with the specified version
optionalDependencies — these dependencies are optional and failing to install them will not break the installation process
bundledDependencies — it’s an array of packages that will come bundled with your package. This is useful when some 3rd party library is not on NPM, or you want to include some of your projects as modules

So what should be the solution then? The problem is about peer dependencies. The solution is to downgrade angular common or the solution is to use legacy dependencies logic for installing packages using --legacy-peer-deps. So --legacy-peer-deps does not try to install the peerDependencies automatically. Is this going to work for you? Probably, yes. But you should add specific instructions how to do that, or to make the use of --legacy-peer-deps automatic for future installation of the project packages with this code from one of the previous answers:

npm config set legacy-peer-deps true

In my case I installed the package and I tried to run ng serve, but because --legacy-peer-deps was used, there were dependency packages which were not installed. I had to install those manually (because I did not set the configuration from the code above). At the end installing about five packages manually, all with --legacy-peer-deps, I ended to a package that could not be installed and I did not try to continue, because my project was throwing warnings like crazy and there were a lot of packages for audit too. So my decision was not to use this package and to find an alternative.

我读到的其他解决方案:

downgrade Node.js to v14. This will downgrade npm. It might not be v14, but this was the version that was most widely downgraded to. Some people use Yarn to force package installation - personally I don't understand how this works, because I haven't used Yarn. downgrading Angular and the global Angular CLI version to version that will satisfy the requirement. In my case it is angular/common, and in the question it's angular/core, but both require downgrading the whole angular right (I am not sure about this here). the package you install might have a higher version that doesn't require downgrading Angular. You might try to use the https://updatepackagejson.com/ to upgrade your packages to the latest, but this is in case your project is quite new.

其他回答

这与HTTP代理无关。

正如上面所说,您存在依赖冲突(不正确且可能被破坏的依赖),因此尝试使用——force或——legacy-peer-deps运行该命令。如果它不生效,临时解决方案是使用以前版本的Node.js(降低Node.js版本),因为它有时会导致这种错误发生。

根据OP的更新更新:

如你所见,它触发了以下错误:

@angular/http@^9.1.4没有找到匹配的版本。

看看angular/http页面。请注意,该已弃用包的最新版本是7.2.16,而您请求的是更高版本(例如,^9.1.4)!因此,请尝试检查项目依赖关系,并根据所提出的错误来解决问题。

在使用npm 7时,这种情况经常出现,因为对等依赖问题在版本7中被视为错误,而在版本6中通常只是警告。通常使用——legacy-peer-deps可以让它在npm 7中工作。

当这不起作用时,一个选择是降级到npm 6。降级Node.js是没有必要的(但也没有害处)。相关的依赖管理代码在npm中。降级Node.js通常是巧合,因为这样做通常也会降级npm。

另一个比降级npm破坏性更小的选择是使用npx来使用之前版本的npm的install命令:npx -p npm@6 npm install

当所有这些都失败时,通常值得尝试删除node_modules目录和包锁。Json,然后再次运行NPM install。这会重新生成node_modules和package-lock.json。

首先要了解问题。以下是我的错误:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: project-admin@11.0.0
npm ERR! Found: @angular/common@11.0.3
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"11.0.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^9.1.0 || ^10.0.0" from @agm/core@3.0.0-beta.0
npm ERR! node_modules/@agm/core
npm ERR!   @agm/core@"3.0.0-beta.0" from the root project

首先,你应该从下到上读这道题。这里@agm/core@3.0.0-beta.0需要通用的angular 9.1.0或10.0.0。最上面的消息说,角的公共发现实际上是11.0.3。

(如果你想更好地理解依赖关系,这里有一个非常简单的网站:如何npm3工作)

dependencies — these are the essential dependencies that you rely on and call in your project’s code
devDependencies — these are your development dependencies, for example, a prettier library for formatting code
peerDependencies — if you set a peer dependency in your package.json, you are telling the person who installs your package that they need that dependency with the specified version
optionalDependencies — these dependencies are optional and failing to install them will not break the installation process
bundledDependencies — it’s an array of packages that will come bundled with your package. This is useful when some 3rd party library is not on NPM, or you want to include some of your projects as modules

So what should be the solution then? The problem is about peer dependencies. The solution is to downgrade angular common or the solution is to use legacy dependencies logic for installing packages using --legacy-peer-deps. So --legacy-peer-deps does not try to install the peerDependencies automatically. Is this going to work for you? Probably, yes. But you should add specific instructions how to do that, or to make the use of --legacy-peer-deps automatic for future installation of the project packages with this code from one of the previous answers:

npm config set legacy-peer-deps true

In my case I installed the package and I tried to run ng serve, but because --legacy-peer-deps was used, there were dependency packages which were not installed. I had to install those manually (because I did not set the configuration from the code above). At the end installing about five packages manually, all with --legacy-peer-deps, I ended to a package that could not be installed and I did not try to continue, because my project was throwing warnings like crazy and there were a lot of packages for audit too. So my decision was not to use this package and to find an alternative.

我读到的其他解决方案:

downgrade Node.js to v14. This will downgrade npm. It might not be v14, but this was the version that was most widely downgraded to. Some people use Yarn to force package installation - personally I don't understand how this works, because I haven't used Yarn. downgrading Angular and the global Angular CLI version to version that will satisfy the requirement. In my case it is angular/common, and in the question it's angular/core, but both require downgrading the whole angular right (I am not sure about this here). the package you install might have a higher version that doesn't require downgrading Angular. You might try to use the https://updatepackagejson.com/ to upgrade your packages to the latest, but this is in case your project is quite new.

尝试以下两种方法来解决此问题:

选项1:删除文件夹“node_modules”和文件“package_lock”。Json: NPM cache clean -force after NPM I -force 选项2:执行npm install——save——legacy-peer-deps命令

这个问题我已经遇到过很多次了。最后我找到了一个解决方案:

npm install react-native-paper  --legacy-peer-deps