Search code examples
angulardependenciespipeline

How to know which version of angular/common to use to satisfy the dependencies?


When I deployed my changes to the master branch, the pipeline failed. If I'm correct, the error indicates a conflict in the versions of @angular/common required by different dependencies in the project. The root project requires @angular/common@"^16.0.0", while ngrx-store-localstorage@16.1.0 requires a peer dependency of @angular/common@"^17.0.4".

Here is the error in the pipeline that failed during npm install:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: dim-angular@0.0.
npm ERR! Found: @angular/common@16.2.12
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"^16.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^17.0.4" from ngrx-store-localstorage@16.1.0
npm ERR! node_modules/ngrx-store-localstorage
npm ERR!   ngrx-store-localstorage@"^16.0.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 /root/.npm/eresolve-report.txt for a full report.

Solution

  • First, you must ensure that npm install works on your local machine. Often the problem comes from the fact that you use ^ to specify the version of your dependency, see more details about semantic versioning here. In this case, you do not have much control over the exact version of dependency and their dependencies, and sometimes, the project breaks. I would recommend removing ^ to control exact versions manually, and running npm outdated from time to time, to see if you want to proceed with updates. Silent automatic updates may give you surprises, such as it worked yesterday, but stopped working today "for no reason".

    Then, you should ensure that your deployment uses the same versions of all the dependencies. This is why the file package-lock.json exists. See here for more explanation. I recommend committing this file to your repository and running npm ci to install the dependencies, see here.

    A similar approach should be applied to yarn package manager or others.