How to Easily Share Code Between Projects with Bit

Jonathan Saring
Share

Sharing code between your repositories and projects is critical your our product’s success.

When your team fails to share code, you end up with duplicated code across your codebase — which is bound to slow down the delivery cycles and make maintenance a nightmare, as every change would require long refactoring in multiple places. You also waste valuable time and effort and reinventing the wheel, instead of building new things.

Share code between projects with Bit

As your team and projects begin to scale, this problem becomes more painful.

The Javascript ecosystem is working hard to make code sharing simpler, with new technologies like Bit, Lerna and more.

Let’s take a quick look at how you can easily share code as a team in both multirepo and monorepo architectures.

The Overhead of Sharing Code

The main barrier to code sharing is overhead.

Let’s say we have a repository, and we want to share part of it as a package.
Traditional workflows force you to split your existing repository into two repos, and use one of them to set up and publish the package. Now imagine you have 500 packages you want to share out of 50 repositories.

When you need to make changes to these packages, you would have to make changes in over 500 repositories which is bound to get very messy very quickly, likely just to end up in dependency hell.

You can use Lerna to keep those packages in a single repository if you choose to restructure your entire project into a huge monorepo. This can help reduce the overhead of making changes in multiple repos, but will still force you to set up and maintain these packages inside the repo, manually handle their dependency trees and still every change would have to go through the original repo’s context as a source of truth.

Regardless of architecture, this type of overhead is often enough to make code sharing all too hard.

Bit breaks this overhead by letting you easily share, change and use code from any repo or project in a fully controlled and scalable way. Let’s see how.

Bit: Faster, Scalable Code Sharing

Bit - the fastest way to share code

Bit breaks the overhead of sharing code as we know it, offering the ultimate code sharing experience for every type of codebase architecture.

Instead of splitting your codebase or forcing you to go monorepo (unless you want to), you can use Bit to share parts of your code directly from any existing repository (as it is) and install them in other projects with npm.

You don’t have to restructure your project at all or configure any package environments. Instead, you can simply point Bit to the components of code (sets of files) you want to share, let it define their dependency tree for you, and then share them right into a shared location called a Scope. You can also add build an test envrionemnts and let Bit make sure your components are ready to deploy to production.

From there, your different code components can be installed using npm and Yarn just like any other package.
Here’s an example of a React Scope containing this Hero component shared from this app on GitHub — which wasn’t changed at all. I also didn’t have to split it into more repos.

Bit - the fastest way to share code

As you can see, the component is now available to my whole team to find and choose based on useful visual information, tests results, live rendering (alpha) and more.

Now comes the really cool part.

Let’s say that after installing this component in another project using npm, I want to change it a little. All I have to do is use the bit import command to bring its actual source code into any path in any of my repos, and change the code. Bit will continue to track the code even when sourced in my projects and update changes across my codebase.

Then I can also simply share it back to the Scope (or to a new one) and even eject the component back to being a package dependency for my project.

Sharing the code took about 30 seconds and did not force any changes to my codebase. Installing it was simple using npm, without Bit. Changing it was even simpler, right from my consuming project’s context.

Conclusion

Sharing code between projects is extremely important, but also complicated.
With Bit, you can easily share any part of your project and use it in other projects any way you choose. You don’t have to restructure you codebase, make messy changes in multiple repos or boilerplate packages.

It’s an open-source project, so feel free to get started, suggest feedback, and learn more!