Unable to resolve dependency tree error when installing npm packages

499,171

Solution 1

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.

Solution 2

Try this command-

npm install --save --legacy-peer-deps

Solution 3

In addition to using the --legacy-peer-deps command line option, this can also be set more permanently as a config option:

npm config set legacy-peer-deps true

Solution 4

When using npm 7, this comes up a lot because peer dependencies issues are treated as errors in version 7 whereas they were generally only warnings in version 6. Usually using --legacy-peer-deps makes it work with npm 7.

When that doesn't work, an option is to downgrade to npm 6. Downgrading Node.js is not necessary (but not harmful either). The relevant dependency management code is in npm. Downgrading Node.js will often work coincidentally because doing so will often downgrade npm as well.

Another option that is less disruptive than downgrading npm is using npx to use the previous version of npm for just the install command: npx -p npm@6 npm install

And when all else fails, it's often worth a shot to remove the node_modules directory and package-lock.json, and then run npm install again. That regenerates node_modules and package-lock.json.

Solution 5

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.
Share:
499,171
Pearl
Author by

Pearl

Updated on July 16, 2022

Comments

  • Pearl
    Pearl almost 2 years

    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