1

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.

2 Answers2

1

I'm not sure the ability to have an npm dependency to a git branch. Maybe its possible but normally you would publish that git repo to an npm repository. Or you can reference the 'main' file like this:

Solution 1 - Import from locally installed npm project

repo1/package.json

  "files": [
    "dist" // the generated production output from this repo
  ],
  "main": "dist/index.umd.js",

You would also have to install npm packages in both repos for this to work.

repo2/package.json

  "dependencies": {
    "@otherrepo": "file:../repo1"
   }

repo2/index.js

import otherrepo from '@otherrepo'

// do stuff with other repo

Note for this solution you would need to configure the the alias @otherrepo in webpack or equivalent.

Not sure if this is what you are after but this is an example I created using 2 repos (webpack and microbundle) The webpack example loads the output from the microbundle as a dependency

https://github.com/inspiraller/webpack-and-microbundle

Solution 2 - publish your git repo to npm

Once you are happy with your development cycle you can publish repo 1 to the npm repository and then you can just install it like any other npm package.

Solution 3 - Use git submodules:

Answered here: How can I have linked dependencies in a git repo?

I'm sure the technology here has advanced, but when I played with it I discovered too many pitfalls of caching and reinstalling.

Solution 4 - load shared components via an external managed cdn

This is for React but there are probably alternatives to other frameworks

Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63
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.

Rachel Cantor
  • 313
  • 3
  • 9