8

I am trying to test a branch of a local module in a separate project.

Normally, I npm link the local module to my dev environment. The module link is always on the master. I'd like to have separate branches and npm link a specific branch of the module in a separate project.

I tried using some syntax in this post such as npm link modlue#nameOfBranch

I know you can install a specific git branch from GitHub based on the npm docs. But ideally, I would like to work on the modules branch, make updates, then have those changes instantly reflected via the npm link in my working project.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
HelloWorld
  • 10,529
  • 10
  • 31
  • 50

1 Answers1

7

If I understand your question correctly then you are already having a project say project_1 which is using another local project say project_2 as a dependency using npm link. Now you are trying to create another new project say project_3 which would be using project_2's different branch as a dependency. You don't want your project_1 from using project_2's master as a dependency.

The best solution can think of is creating another new project which would be the replica of project_2 name it as project_2' and then start working on a different branch and use npm link to use it as dev dependency. So whenever you update it locally you can find the changes without having to update it.

You cannot work with the same project_2 because once you check out the branch then git changes at the file system level. No two projects can work with different branches.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Very Close! I have `project_1` that is used as a dependency in `project_2` via `npm link`. I want to use a specific branch that I'm working on from `project_1` in `project_2`. Right now with `npm link` it only reads from master, ideally, I can have it read from my working branch in `project_1`. – HelloWorld Oct 15 '18 at 19:03
  • The best solution I can suggest would be to use a seperate project which can be on a different branch. Can you try this solution? – Arghya Saha Oct 15 '18 at 19:12
  • I tried this out (still tinkering with this) and don't know what to do at this step: I made a copy of my dependency, created a new branch where I am adding new features, then "and then start working on a different branch and use `npm link` to use it as dev dependency". When I run `npm link` from the new feature branch, it's defaulting to that projects "master" branch and not the new-feature branch I was in when I ran `npm link`. Are you describing in the copied project, just to work in the master branch the whole time? – HelloWorld Oct 16 '18 at 15:27
  • I don't think there is a way to use `npm link` the way I want. Instead, I made an npm script to copy the local dependency into my main project. When I copy the local dependency I am working on, it is in the new-feature branch. When I copy the depedency with the npm script, it copies the folder in the new-feature branch, not the master branch. I am not sure why npm link doesn't behave like this, maybe I set it up wrong, I tried it in a mini project as well and it seemed it only read from the master branch. – HelloWorld Oct 16 '18 at 16:14
  • The issue might be, while you were in the master that time you created the `npm link` now once you made changes and edited then the old link is persisting. So you should repeat the whole process of creating the global link and then linking it in the new project. Probably it would solve your problem. Can you try this? – Arghya Saha Oct 16 '18 at 17:26