Pearl
Pearl

Reputation: 9445

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

Upvotes: 804

Views: 1704881

Answers (30)

Ali Hasan
Ali Hasan

Reputation: 4353

Try the command flag --legacy-peer-deps.

This option is used when it encounters compatibility issues with peer dependencies in the project. Peer dependencies are a way for a package to specify that it relies on another package but doesn't want to include it as a direct dependency. This allows multiple packages to depend on a common package without having multiple copies of it in your project.

The --legacy-peer-deps option tells the package manager (npm or Yarn) to use an older, more lenient approach when resolving and installing packages with peer dependency conflicts. It can be useful if you're working with older packages that haven't updated their peer dependency definitions to align with newer package manager behavior.

npm install --legacy-peer-deps

Upvotes: 396

Cyebukayire
Cyebukayire

Reputation: 947

I did this command and it worked for me on MacBook Pro 2016.

npm install --legacy-peer-deps

Then

npm install

Upvotes: 2

Marsilinou Zaky
Marsilinou Zaky

Reputation: 1047

If you are utilizing NPM, you can leverage the new overrides feature. By specifying the desired version of a dependency in the overrides section of your package.json, you can enforce its usage.

Check the official documentation for more details: NPM Overrides

Example:

"overrides": {
  "next": "^14.1.1-canary.59"
}

Upvotes: 0

Jahedul Hoque
Jahedul Hoque

Reputation: 247

Here, most of the answers indicate two solutions:

npm i --force

and

npm i -legacy-peer-deps

Surely they are the quick solution. Let's understand what they do actually.

The npm i --force command is used with Node Package Manager (npm) to install packages forcefully. When you run this command, npm will install the specified packages, even if they are incompatible with the current version of your project or if there are potential conflicts with other dependencies.

When you use npm i --legacy-peer-deps, npm will use the older, more flexible algorithm for resolving peer dependencies, which is similar to how peer dependencies were resolved in npm versions 4 and earlier. This can be helpful in situations where packages haven't been updated to declare compatibility with the stricter peer dependency rules introduced in npm 5. Also, this problem still exists in your project. Whenever you try to install any other project, you have to extend the installation the --force or -legacy-peer-deps

Both options are not recommended if there is any other unbreakable solution.

I also used this in most of my projects. But recently, I tried a different way. Let me explain what I did.

  1. delete the node_modules and package-lock.json.
  2. run npm cache clean --force
  3. listed all the packages from package.json to a notepad.
  4. Then removed the dependencies from package.json
  5. Later, install all the dependencies manually, like npm i package1 package2
  6. After that, I had to fix some version upgrade issues, but finally I got rid of this problem.

Upvotes: 7

Shimi Shimson
Shimi Shimson

Reputation: 1090

On Windows 10, npm 8.19.3, all I had to do was to first remove the package which caused the problem (in my case, it was @angular/material) and then update my Angular version (with ng update @angular/cli), then install my package and everything went fine.

The problem was, I had Angular version at 15.something, while npm install @angular/material was trying to install 16.something of itself and that caused dependency issues.

Upvotes: 0

Astrophage
Astrophage

Reputation: 1439

We had the same issue, resulting in the error below:

npm ERR! code ERESOLVE npm
ERR! ERESOLVE could not resolve npm
ERR! npm
ERR! While resolving: @angular/[email protected] npm
ERR! Found: @angular/[email protected] npm
ERR! node_modules/@angular/material npm
ERR! @angular/material@"~12.0.4" from the root project
...

We use npm ci for clean install in Azure Pipelines.

The issue was very often that package.json and package-lock.json were not in sync anymore.

The solution to it was to execute npm install local and push the new package-lock.json.

As an additional hint, we added a new task in the pipeline for additional information if the job fails.

- task: Npm@1
   displayName: npm install
   inputs:
     command: custom
     customCommand: ci
     customRegistry: useNpmrc

 # ##vso[task.logissue type=error] writes the text to the summary page (error-log).
 - bash: echo "##vso[task.logissue type=error] If 'npm install' fails with 'ERESOLVE could not resolve', 'package.json' and 'package-lock.json' (needed for 'npm ci') may be out of sync. Run 'npm install' locally and push the new package-lock.json."
   condition: failed() # Only execute on fail
   displayName: npm install failed hint

Upvotes: 1

tony
tony

Reputation: 1008

Disclaimer: This assumes you're on npm v7+

Note: If you follow the instructions of sibling commenters, it will create a user-scoped config that won't sync consistently across teammates / machines / buildbots.

Project-based legacy peer dependencies

You will probably want legacy-peer-deps tied to your project so it proliferates across machines / developers, and doesn't contaminate your other projects.

npm config set legacy-peer-deps true --location project

This will create a local file at .npmrc which you can commit to your repository:

legacy-peer-deps=true

Then afterwards, you can just run:

npm install

Then commit the updated lockfile.

Remember, location, location, location:

  • per-project configuration (/path/to/my/project/.npmrc, see more):

    npm config set legacy-peer-deps true --location project
    
  • per-user configuration (defaults to $HOME/.npmrc, see more)

    npm config set legacy-peer-deps true --location user
    

    or, as the default location is user anyway:

    npm config set legacy-peer-deps true
    
  • global configuration (defalts to $PREFIX/etc/npmrc, see more)

    npm config set legacy-peer-deps true --location global
    

    or, as --global infers --location global

    npm config set legacy-peer-deps true --global
    

For some projects, fixing dependencies may be non-trivial

In my case, a critical dependency we have a legacy version of wants to pull in webpack v3 (!) - but that's a build dependency of that project's.

The best solution on a short term basis is to use legacy-peer-deps as a hold over.

If you are in a pinch, you could also consider forking the dependency and adjusting its peer dependencies accordingly - then point your project to the fork.

Upvotes: 16

Rizwan
Rizwan

Reputation: 441

You can install the packages using two ways it is showing this error:

ERESOLVE unable to resolve dependency tree
  1. Install the package using npm install and having --legacy-peer-deps

    npm install --save --legacy-peer-deps
    
  2. This is a combination of two commands

    a. Set legacy-peer-deps true in npm config

    npm config set legacy-peer-deps true
    

    b. Now install packages using npm install

    npm install
    

Upvotes: 10

Hrishikesh Kale
Hrishikesh Kale

Reputation: 6568

I was getting this error on GitHub actions while pushing my updated package and package-lock JSON file.

The issue was in our inconsistency in-house NPM packages. I had to add

legacy-peer-deps=true

in our .npmrc file to override the dependency.

So I feel sometimes it's better to use legacy-peer-deps to override unwanted dependencies in your project packages.

Upvotes: 2

user9869932
user9869932

Reputation: 7377

In my case, I started getting the error (below) after upgrading npm from version 6 to 7.

npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree

...

npm ERR! Fix the upstream dependency conflict, or retry this command with --force, or --legacy-peer-deps to accept an incorrect (and potentially broken) dependency resolution.

In my case compiling with either --legacy-peer-deps or --force flags resulted in a useless bundle.

So I tried deleting the node_modules, package-lock.json, and bundle using yarn install. This generated a yarn.lock file and created package-lock.json that worked fine in subsequent npm runs.

P.S.: I am using the temporary workaround until npm 7 works fine with my project: after that, I will delete yarn.lock, package-lock.json and folder node_modules, and recompile with npm

rm -rf node_modules
rm package-lock.json
yarn install
# Generates a yarn.lock file and a new package-lock.json

# Continue with npm
npm start

Upvotes: 3

tom
tom

Reputation: 10601

This happens for some packages after updating to npm 7.

Parameter --legacy-peer-deps can help:

npm i --legacy-peer-deps

Described here legacy-peer-deps

Causes npm to completely ignore peerDependencies when building a package tree, as in npm versions 3 through 6.

If a package cannot be installed because of overly strict peerDependencies that collide, it provides a way to move forward resolving the situation. ...

You can set this option to true by default (not recommended by npm):

npm config set legacy-peer-deps true

Or just wait until these packages get up to date.

Upvotes: 63

friederbluemle
friederbluemle

Reputation: 37177

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

npm config set legacy-peer-deps true

Upvotes: 143

MTN
MTN

Reputation: 101

Deleting the node_modules folder helped me, after which I ran the command npm i --force

Upvotes: 0

Chamila Maddumage
Chamila Maddumage

Reputation: 3876

npm install --save --legacy-peer-deps word for me with Angular 13.

Upvotes: 2

Vinit Dabhi
Vinit Dabhi

Reputation: 1089

First, execute this in your terminal.

npm config set legacy-peer-deps true

Second, clear the cache:

npm cache clean --force

And finally, execute your command.

Upvotes: 70

Val
Val

Reputation: 65

Downgrading Node.js is the process of installing a previous version of the Node.js software on your system. Downgrading to version 14 can be useful if you have compatibility issues with a newer version of Node.js, or if you want to use a version of Node.js that you are already familiar with. The steps to downgrade Node.js to version 14 are as follows:

  1. Source your bash profile: Before you can downgrade Node.js, you need to make sure that you have the necessary environment variables set up. The first step is to source your bash profile using the following command:

source ~/.bash_profile

  1. Use nvm to switch to version 14: The next step is to use the Node Version Manager (nvm) to switch to version 14 of Node.js. You can do this by using the following command:

nvm use v14.16.1

Note that the version number v14.16.1 is just an example. You can use any other version number of Node.js v14 that you have installed on your system.

  1. Install npm: Finally, you need to install the Node Package Manager (npm) so that you can manage packages and dependencies in your Node.js project. You can do this by using the following command:

npm install

After running these commands, you should have Node.js v14 installed on your system, and you can start using it to build your applications.

Upvotes: -1

Rigers Leka
Rigers Leka

Reputation: 483

NPM can be used to install and manage versions of dependencies in your projects.

I had it the same issue on React versions in relation with the npm version:

npm error found types/[email protected]

So it might be package-versions that need to be installed based on your package.json file.

It gives errors in the npm 7 version and cannot install Node.js modules.

If you will downgrade npm version to 6, those problems will become warnings and the problem will be resolved.

  • Try to prove this command: npm install -g npm@6

  • Check if version is already installed: npm --version

  • Remove and install node_modules package:

    a) Remove rm -rf node_modules

    b) Install: npm i

Upvotes: 15

aakash sharma
aakash sharma

Reputation: 159

Try two options to resolve this issue:

  • Option 1: Delete folder node_modules folder and file package_lock.json after running: npm cache clean --force after npm i --force

  • Option 2: run npm install --save --legacy-peer-deps

Upvotes: 10

samridhgupta
samridhgupta

Reputation: 1745

This is an issue of Node.js version. Some latest versions of Node.js could show errors like these.

https://github.com/nvm-sh/nvm

I use NVM to manage Node.js versions on the system and use Node.js 12 to get past this error.

Command to change version:

nvm use 12

Upvotes: 0

komal dubey
komal dubey

Reputation: 57

Use

npm install --legacy-peer-deps

This worked for me.

Upvotes: 2

Julio Cachay
Julio Cachay

Reputation: 830

For this case, I was having the issue

ERESOLVE unable to resolve dependency tree

in an Angular 13 project that used some packages from a private npm feed in Azure DevOps.

To access this repository, I created an .npmrc file. Because of this, the npm install command would search all packages in my private repository and not in npm feed any more. The unable to resolve the dependency tree error happened because the npm install command could not find many of the packages that were hosted in the npm feed and not my private feed.

I found this amazing answer on how to scope packages.

Based on this, I made some changes:

  1. In my library Package.json, update the name to have a scope name @mylib

    "name": "@myLib/command-queue",
    
  2. Build and publish this package to my private feed

  3. In my client app (the one that uses this package), update the .npmrc file to use my private feed for packages in this scope only

    @myLib:registry=https://pkgs.dev.azure.com/...
    always-auth=true
    

Now, whenever I run the command npm install, if the package has the scope @myLib, it will look for it in my private feed, and use the npm feed for all other cases (i.e., @angular/...)

This is an example of my client app Package.json file:

    "@angular/platform-browser-dynamic": "~13.3.0",
    "@angular/router": "~13.3.0",        <-- this comes from npm
    "@myLib/jcg-command-queue": "^2.2.0", <-- This comes from my private feed

Also, with this change, there isn't any need to add --legacy-peer-deps to the npm install command any more.

Upvotes: 1

makkasi
makkasi

Reputation: 7318

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.

Upvotes: 384

Yarn has a feature for solving this. If you can, try to use it for installing the package.

Upvotes: -4

Aly
Aly

Reputation: 5041

The problem is related to a dependency conflict or broken dependency. You can proceed by accepting the incorrection of dependency by forcing an install.

Solution: Using command with --force.

Your command will be like npm install --force @your-npm-package.

Note: You can use yarn to install a dependency if it's available in to install with the yarn package manager.

Upvotes: 16

Mazhar k
Mazhar k

Reputation: 251

I have faced this issue many times. At last I found a solution:

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

Upvotes: 2

AlbertSawZ
AlbertSawZ

Reputation: 133

In my case I was having trouble with a @babel/core dependency, but I didn't want to use --force, because I was not sure about the consequences, so I went to https://www.npmjs.com/, looked for the package and replaced my old version with the newest one. That did the work.

Upvotes: 0

Qin Chenfeng
Qin Chenfeng

Reputation: 471

First I tried

npm install

It gave me error unable to resolve dependency tree and based on the help information from this command,

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.

I tried this command:

npm install --legacy-peer-deps

And it solved my problem.

Upvotes: 11

LitileXueZha
LitileXueZha

Reputation: 616

The fastest solution: npm install --legacy-peer-deps

Explanation:

In npm versions 3 through 6, peerDependencies were not automatically installed, and would raise a warning if an invalid version of the peer dependency was found in the tree. As of npm v7, peerDependencies are installed by default.

npm docs: peerDependencies

Your dependency contains some peerDependencies that conflict with the root project's dependency.

As it described in the npm ERR log.

Upvotes: 8

Prithu Shahi
Prithu Shahi

Reputation: 51

  1. If you have node_modules folder and package-lock.json file in your root directory then remove those:

    rm -r node_modules
    rm package-lock.json
    
  2. Then run commands:

    npm install --save --legacy-peer-deps
    npm audit fix --force
    
  3. Create .env file in the root directory and paste below code:

    SKIP_PREFLIGHT_CHECK=true
    
  4. Now, start your project:

    npm start
    

Upvotes: 2

Farbod Aprin
Farbod Aprin

Reputation: 1186

I just update my Node.js and it works for me:

node -v

Output:

V xxxx

And:

sudo npm install -g n

(Use this command to install the stable node release.)

sudo n stable

Upvotes: 2

Related Questions