5 Practical Ways To Share Code: From NPM To Lerna, Git Submodules and Bit
Tags: github, npm, devops
Published: 2019-08-15 10:37:56


Between multiple repositories, monorepos and micro-services the discussion about sharing common code between projects is increasingly raging.

Sharing common code between projects and repositories is a key factor for better modularity and faster development, but it’s also [complicated](https://medium.com/@jeffwhelpley/the-problem-with-shared-code-124a20fc3d3b). I’ve [written about it before](https://medium.com/p/85c51b2d0049/edit), sharing from our own experience as a team.

Here is a practical overview of 5 methods for sharing code between these repos and projects for 2018. Just remember, down the line it’s really all about communication between people, culture and keeping [modularity in mind](https://addyosmani.com/first/).

## 1. Bit

<center><iframe width="560" height="315" src="https://www.youtube.com/embed/E5lgoz6-nfs" frameborder="0" allowfullscreen></iframe></center>

[Bit](https://bit.dev) ([GitHub](https://github.com/teambit/bit)) is a popular JS tool that manages **both** source-code changes and dependencies for shared components. It’s a powerful way to share code as a team, and keep it synced across different apps and projects.

Bit’s ability to isolate and share code from any repo to another, while maintaining universal control over changes and updates, makes it a very scalable way to increase code-sharing as a team, and reduce overhead.

Bit lets you seamlessly isolate and share modules from any repo, while it wil automatically define their environments and dependency tree. The result is almost instant publishing of components from any repo, with 0 refactoring.

![UI components as Bit modules: instantly share and reuse anywhere](https://cdn-images-1.medium.com/max/2734/1*us5tsKlh8qsxIYhoMtpTSw.png)*UI components as Bit modules: instantly share and reuse anywhere*

Then, you can install components in other projects with NPM/Yarn, or use Bit to import and develop the code right from the consuming project itself. When changes are made, Bit helps you update the versions and merge the changes.

It’s an [open source project](https://github.com/teambit/bit) and a [component-platform](https://bit.dev) which together helps teams turn reusable code units into shared-components.

![](https://cdn-images-1.medium.com/max/2000/1*uvhKCvXM3IyNlSZVrBvMRQ.gif)

Bit’s platform ([bit.dev](https://bit.dev)) also provides discoverability and collaboration over the components you share, which are organized in visual collections in the platform with per-component testing, build, visual rendering and more.

![](https://cdn-images-1.medium.com/max/2000/1*kg9Avc-5WQb2G2PQxHRPIg.gif)

Here is an example [React project](https://github.com/itaymendel/movie-app) and a [matching collection](https://bitsrc.io/bit/movie-app) of components.

![](https://cdn-images-1.medium.com/max/2000/1*Gxjhw2nGfLKMDnEPan28lQ.gif)
[**Share reusable code components as a team · Bit**
*Easily share reusable components between projects and applications to build faster as a team. Collaborate to develop…*bit.dev](https://bit.dev)
[**teambit/bit**
*Easily share code between projects with your team. - teambit/bit*github.com](https://github.com/teambit/bit)

## 2. NPM with / without Lerna

![Sindre Sorhus with 1000 NPM packages](https://cdn-images-1.medium.com/max/2000/1*c2O5IDMtX1FWCFGNYdbQHQ.png)*Sindre Sorhus with 1000 NPM packages*

[NPM](https://www.npmjs.com/) is an amazing thing that happened to [JavaScript](https://hackernoon.com/tagged/javascript), opening the door for code reuse and collaboration around reusable modules and libraries. With all the complaints about this ecosystem, we couldn’t imagine life without it.

As you probably know all about NPM, let’s focus on a few limitations and how to overcome them. Making smart choices from the get-go saves time later.

![[Dan Abramov](undefined) favors many modules](https://cdn-images-1.medium.com/max/2000/1*juDVlPkfmsV-mZw4pqVBQw.png)*[Dan Abramov](undefined) favors many modules*

First, it can be hard to setup and maintain many repositories for many packages. For this reason, some projects are built as multi-package repositories, also known as “monorepos”. Tools like Bit and Lerna (see below) can help you turn projects into multi-package repos, you can learn more [here](https://blog.bitsrc.io/monorepo-architecture-simplified-with-bit-and-npm-b1354be62870).

This also leads many teams to choose shared-libraries as a solution for sharing many smaller components (see below), as it’s hard to have a package for each.

Second, when someone else publishes a package you are limited in your ability to develop it, often leading to a pull-request to the packages’ repo. Tools like [Bit](https://bit.dev) help mitigate this problem, as it lets you bring the component right into any repo, make changes and share a new version.

Third, there is a discoverability problem as you scale. It’s hard to find and choose from many small packages, which leads to wiki sites and long docs. Rollup’s author [Rich Harris](undefined) [wrote](https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4):
> “Many blog posts — scratch that, entire websites — have been created to try and mitigate the difficulty of finding what you need on npm…”
> *“*It’s up to you to evaluate the library: does it have tests? Can you understand the source code? Is it actively maintained? Is the documentation easy to find and consult?*”*

### Lerna

![The Hydra from Lerna: Looking at its tail, I think it’s about to fall down](https://cdn-images-1.medium.com/max/2000/1*obV0EGRGtftMYtM4_DTTQA.png)*The Hydra from Lerna: Looking at its tail, I think it’s about to fall down*

Keeping different packages in different repositories can quickly get out of hand and make it very hard to update changes across your projects.

[**Lerna](https://github.com/lerna/lerna)** helps you to keep and configure multiple packages in a single repository. It can help mitigate the pain of different repos for different packages, and help build and test the project as a whole. This way, you don’t have to keep and maintain separate repos for different packages.

To learn more about going monorepo check this out:
[**Monorepos Made Simpler **
*How to leverage Bit + NPM to simplify monorepo architecture, with and without Lerna and friends.*blog.bitsrc.io](https://blog.bitsrc.io/monorepo-architecture-simplified-with-bit-and-npm-b1354be62870)

## 3. Shared and common libraries

The advantage of [shared-libs](https://blog.bitsrc.io/11-react-component-libraries-you-should-know-178eb1dd6aa4) is that you keep all your shared code in one repo, which is easier to maintain and distribute than multiple micro-packages. However, unlike Lerna monorepos, they will be consumed as one package.

Keeping all your shared code in one repo forces users to add the entire library to their project with redundant code, dependencies, complexity and weight, all to use a single component.

![A React MaterialUI button component — do I really need a whole library?](https://cdn-images-1.medium.com/max/2000/1*pGVKa3pT3yU4IA121MpUuA.png)*A React MaterialUI button component — do I really need a whole library?*

It also makes the process of updates and modification very cumbersome, as every change requires the owner of the project to update the package. This can [impair the adoption](https://hackernoon.com/how-we-started-sharing-components-as-a-team-d863657afaca) of these libraries within the organization. Discoverability for different components within the library is also an issue.

These problems led communities such as Lodash to work long and hard in order to [publish their components](https://knpw.rs/blog/using-lodash/) as [individual packages](https://www.npmjs.com/browse/keyword/lodash-modularized) to NPM. Google’s [Polymer project](https://www.webcomponents.org/) (by [Eric Bidelman](undefined) and other OSS wizards) also keeps over 100 web elements in over different 100 repositories.

Lerna can be used to separate components within the library into packages within the repo. Bit can be used to share components from existing libraries.
[**How Do We Really Use Reusable Components?**
*I’ve talked to 30 teams about their reusable components. Here’s what I’ve learned.*blog.bitsrc.io](https://blog.bitsrc.io/do-we-really-use-reusable-components-959a252a0a98)
[**Shared Components Best Practices for 2019**
*Successful practices for developing and managing shared components between teams and projects with Bit and friends.*blog.bitsrc.io](https://blog.bitsrc.io/shared-components-best-practices-for-2019-c259cdc3b623)

## 4. Git sub-modules

![Lessons learned in the trenches?](https://cdn-images-1.medium.com/max/2000/1*EV6vMxe-a6guNDT448cYdg.png)*Lessons learned in the trenches?*

Submodules are effectively separate repositories within the directory tree of their parent repository. The only linkage between the parent and the submodule is the recorded value of the submodule checked-out SHA, which is stored in the parent’s commits. Changes in that recorded SHA are not automatically reflected in the submodules.

However, submodules have many, many problems. Just run a quick “git submodules” search on Google, and you won’t like the results. Among the major issues, you can find the simple fact that submodules don’t manage the dependency relationships between the modules. Smaller issues include issues like the fact that** **git submodule doesn’t work well with that parent, because a `pull` to the parent directory doesn’t automatically reflect in the submodule.

Also, git doesn’t save the pointer to the submodule when resolving conflicts. This means that if you don’t update the pointer manually you will lose resolved conflicts when merging changes. When you commit to submodule, git will leave you with a detached head in the parent because it is referring to an old “head” which is different from the current “head”. and guess what? When you push parent or submodules other parents don’t get exported.

There are various tools that provide additional automation around the Submodule feature, such as [git-subtree, gitslave, braid and giternal](https://codeburst.io/4-git-submodules-alternatives-you-should-know-592095859b0). But as of today, the only tools that manage both source-code changes and dependencies for modules across projects and repos is [Bit](https://bit.dev), in JS.

## 5. Copy-pasting code
> Because let’s face it, what did future me ever do for me?

![When your teammate yells YOLO and goes rouge](https://cdn-images-1.medium.com/max/2328/1*dw74IhuKKn24urhu7k0mbw.png)*When your teammate yells YOLO and goes rouge*

To be honest, it is perhaps the most commonly used practice for code “reuse” on earth. I’d like to think that in most cases it’s the result of the lack of a “cheap” effective alternative and somewhat chaotic delivery cycles.

The problem is, code duplications are not cheap at all. Far from it.

![A classic problem for future me.](https://cdn-images-1.medium.com/max/2000/1*Jp03biZSsb5To7-WOlWMBA.png)*A classic problem for future me.*

Duplications are an ever-growing debt in your code base. You will quickly lose control, making maintenance a nightmare and delivery cycles ever longer. Many problems will only be discovered after delivered to production.

A recent study found that half the code on GitHub [is duplicated](https://www.theregister.co.uk/2017/11/21/github_duplicate_code/). Our research found that a Javascript function like is-string was duplicated over 1,000 duplications of 100 different implementations in only 10k GitHub repos.

Think about the new features that could have been written if more people would share code, rather than duplicate or reimplement it. Think about the costs of maintenance and long delivery cycles for your organization.

## Sharing code is about people

Each project and every developer has their own set of concerns, tools and workflows. Still, sharing code is the key to true modularity, which becomes increasingly popular in todays’s ecosystem and provides great advantages.

We chose to build [Bit](https://github.com/teambit/bit). No matter what method and tooling you choose, it’s important to encourage a culture that breeds sharing and collaboration.

After all, sharing between projects starts with sharing between people.