Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve dependency tree error when installing npm packages

When trying to install the npm packages using npm i command, I am getting the following exception:

Enter image description here

I have tried reinstalling the Node.js package and setting the proxy to off using:

set HTTP_PROXY=
set HTTPS_PROXY=

The issue is still there. What I am doing wrong?

Update:

When I run the following command:

npm install --legacy-peer-deps

The following error is displayed:

Enter image description here

like image 212
Pearl Avatar asked Oct 15 '22 11:10

Pearl


People also ask

Can't resolve dependency while npm install?

The easiest way to fix the issue is to pass an additional parameter –legacy-peer-deps to npm install. The --legacy-peer-deps tells the npm to ignore the peer dependencies and continue the installation of the package. Try the below command to install the dependencies for your project.

How do you fix npm err Eresolve unable to resolve dependency tree?

remove node_modules and package-lock. json . I had a newer version (14) of Angular on my PC compared to the version the app was using (13) and this worked perfectly.


3 Answers

This is not related to an HTTP proxy.

You have dependency conflict (incorrect and potentially broken dependency) as it says, so try to run the command with --force, or --legacy-peer-deps. If it doesn't take effect, the temporary solution is using prior versions of the Node.js (downgrading the Node.js version) as it causes this kind of errors to happen sometimes.

Update based on the OP's update:

As you see, it fires the following error:

No matching version found for @angular/http@^9.1.4.

Take a look at angular/http page. Note that the latest version for that deprecated package is 7.2.16 while you request an upper version (e.g., ^9.1.4)! So, try to check the project dependencies and follow the raised errors in order to solve the problem.

like image 616
Amirhossein Mehrvarzi Avatar answered Oct 17 '22 01:10

Amirhossein Mehrvarzi


Try this command-

npm install --save --legacy-peer-deps
like image 274
Ali Hasan Avatar answered Oct 17 '22 03:10

Ali Hasan


First to understand the problem. Here is what I have as error:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: @angular/[email protected]
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/[email protected]
npm ERR! node_modules/@agm/core
npm ERR!   @agm/core@"3.0.0-beta.0" from the root project

First you should start to read the problem from the bottom to the top. Here @agm/[email protected] requires angular common 9.1.0 or 10.0.0. And the top message says that the angular common found is actually 11.0.3.

(If you want to understand dependencies little bit better, here is very simple site: How npm3 Works)

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.

Other solutions that I read about along the way:

  • 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.
like image 184
makkasi Avatar answered Oct 17 '22 03:10

makkasi