Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS: Working with multiple Git repositories

We have a node.js project with a few modules which spans across multiple Git repositories. The modules have dependencies between them.

For example:

common module resides in its own repository. execution module resides in a separate repository, and has a (npm) dependency on common.

So, the directory structure in execution includes (once npm install is executed) common under node_modules.

Our problem is, when developers are working on execution, they some times need to modify common as well. In order to 'see' their changes, they have one of two options we currently use: Either modify node_modules/common (which is ugly, untracked, etc.), or modify the common repository, then push+npm install their changes (which, while cleaner, is quite cumbersome).

What we're wondering is if there's a better alternative for this work process...

like image 496
Gadi Avatar asked Mar 23 '15 08:03

Gadi


People also ask

Can I have multiple Git repositories?

With Git, using multiple repositories is the only way to work efficiently. This enables each team to work independently, and do their work faster. You can also make sure that developers only have access to the repositories they need access to (thus making Git more secure.)

When should I use multiple repositories?

A multi repo can better support granular access control and configuration changes. If you have a large team that collaborates on a complex infrastructure system, multiple source repositories allow you to localize changes and lessen the blast radius of failed infrastructure updates across the system.


2 Answers

You could avoid modifying your package.json file by using the npm link command. This will automatically configure the execution package to symlink its node_modules/common directory to your local clone of common.

How to use this method

  1. Inside your common directory type: npm link. This will create a global reference to your common folder in npm, identified by common (assuming common is the exact name of your node package).
  2. Inside your execution directory type: npm link common. This will symlink the node_modules/common folder into your local copy of the common directory.

When you're done making changes to your common folder, you need to update that package online, and then change the package.json of execution to point to the updated version of common.

I prefer this method over the one suggesting to point package.json to your local repository, because that leaves your package.json in an invalid state that you might accidentally commit and push.

like image 121
Rotem Harel Avatar answered Oct 28 '22 16:10

Rotem Harel


I assume you are using them as a dependency using package.json, which allows you to require them with the name and not the path. (You also have goodies of the version handling with this.) . I couldnt find any option to remove the step of npm install but I did found something which can remove the step of git push and will make your npm install faster.

Local dependencies.
Using local paths as dependencies which can be your some other git repo, you can make changes directly to the git repo of your dependency. This allows you to change the code and test it without pushing. (Although you have to do npm install again in the main module, which will duplicate the working copy of your dependency in your node modules).

Word of caution : You have to take care that you push the final changes made to your dependency code once you have finished working on both, otherwise other developers might go in inconsistent state.

How to add local dependencies

 "dependencies" : {
       "here" : "file:./test/git/repo/here#0.0.1"
 }

Note that this local dependency feature was added to npm in version 2.0. So you might need to update your node if you dont already have npm 2.0+

like image 30
Vishwanath Avatar answered Oct 28 '22 17:10

Vishwanath