Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git submodule vs npm package?

I'm using git submodule to build and shared components between projects. The project is not in production yet, so, at this point submodule is serving well.

But I'm concerned about maintenance and deploy, would be a good idea transform it into a npm package ?

like image 540
Giulia Lage Avatar asked Jun 26 '20 16:06

Giulia Lage


People also ask

Why do we need git submodule?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.

Can a submodule point to a branch?

You can set the submodule to track a particular branch (requires git 1.8. 2+), which is what we are doing with Komodo, or you can reference a particular repository commit (the later requires updating the main repository whenever you want to pull in new changes from the module – i.e. updating the commit hash reference).

What is git submodule update?

A git submodule update will bring the latest commits into your local Git worktree. In this git submodule update example, we'll show you how branches can seem out of sync between your submodule and the latest commit, and how to issue the appropriate git command to update those git submodules with the latest code.

What is git recurse submodules?

If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

What is the difference between GIT and NPM?

Developers describe Git as "Fast, scalable, distributed revision control system". Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. On the other hand, npm is detailed as "The package manager for JavaScript".

Should I use Git submodules for all projects?

With git submodules, you have all the source in one folder. If it's at all possible, I'd recommend using a plain monorepo for all projects. You may need to create build time variables (via babel plugin/s), you may need some sort of "live config" get served from the backend.

What is the difference between GIT and a package manager?

The git solutions seem to be a lot more hassle than a simple package manager. This is not about hassle. through binary dependencies, with a package manager ( Nexus, or Conan for C++: you declare your dependencies, and the package manager fetches them and uses them during the compilation.

Is there a better alternative to NPM?

We tend to stick to npm, yarn is only a fancy alternative, not 10x better. Using a self-hosted private repository (via sinopia/npm-mirror) make package locking (mostly) pointless.


1 Answers

An npm package will allow fragmentation across different package versions. On the other hand, git submodules have a bit of a learning curve, and the tooling is really not that good. With git submodules, you have all the source in one folder.

If it's at all possible, I'd recommend using a plain monorepo for all projects. You may need to create build time variables (via babel plugin/s), you may need some sort of "live config" get served from the backend. I worked with git submodules for a year and I've recently worked with a project that uses npm to share code.

I would recommend using only one git submodule, for all shared code, instead of several submodules. I would strongly consider using lerna, and use your one git submodule to track lerna's packages directory. And if the team decides they don't like git submodules, you can easily make this repo a sibling git repo, instead of a submodule. However, above all this, I'd recommend using a plain monorepo.

Here's a great talk on monorepo's from Netflix: https://www.youtube.com/watch?v=VNqmHJtItCs (strong focus on discouraging npm-style packages)

Here's google's infamous monorepo talk: https://www.youtube.com/watch?v=W71BTkUbdqE

This is a great site to read to help you think about good development flows: https://trunkbaseddevelopment.com/ (it primarily advocates for the monorepo approach)

If you are developing software for different clients(different people/companies paying you for similar projects), and have some agreement that they should be at least ~80% the same, you may really enjoy using build flags to help get started on splitting functionality, but I'm sure you should very proactively keep the code around the build flags clean, and refactor into re-usable components/packages. Give each client some sort of build-flags.json. Build flags should be named for features only, which in theory can all be individually toggled. Some code may be totally custom for each project, in this case, you may want to consider using dynamic imports, but generally this is a pain point I have yet to fully cross, although I have plenty of unrefined ideas around this.

If a monorepo is just not happening, I would actually recommend using npm packages+separate repos over git submodules, assuming you can do good semantic versioning of the package. (And, yalc seems to be a good tool for linking together packages, as opposed to the standard npm/yarn link)

like image 87
Devin Rhode Avatar answered Oct 25 '22 12:10

Devin Rhode