'Wrong git branch loaded in node_modules by yarn install command

I have two Typescript projects.

Context: ProjectA depends upon a certain ProjectB branch project

ProjectB git branches:

  1. main
  2. new_branch

ProjectA package.json

"dependencies": {
    "@projectB": "git+https://github.com/projectB.git#new_branch",
}

What I want to achieve? When I execute yarn install to make sure that the dependencies from the new_branch are loaded in node_modules folder of projectA.

What actually happens? When I execute yarn install, the dependencies from main branch are loaded.

What I tried?

  1. I deleted node_modules & build folders and tried to run yarn install again which did not work.
  2. I deleted yarn.lock but the project split another errors that are not related to my changes from the new_branch at all.
  3. I deleted the @projectB: git+https://github.com/projectB.git#new_branch dependency from the package.json file and I added it via yarn add projectB@git+https://github.com/projectB.git#new_branch which did not work.


Solution 1:[1]

The reason is because you should prefix the branch name with either #branch=new_branch or #head=new_branch. Additionally if you wanted to use a particular workspace, you could append that to the URL as well:

https://github.com/projectB#branch=new_branch&workspace=@projectB/workspace-you-want-to-install-from-new_branch

Another way to link things in a more specific way is to do a commit SHA:

https://github.com/projectB#commit=f4f78b319c308600eab015a5d6529add21660dc1&workspace=@projectB/workspace-you-want-to-install-from-that-commit-sha

If at anytime you need to troubleshoot, you should search your yarn.lock to understand what the package.json is being resolved to under resolution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rachel Cantor