I write code and words. Working on github.com/teambit/bit
Setting up a Node.js package registry is often the first step towards scaling code-sharing and making life a little bit easier for your team.
Sharing more modules means duplicating less code. It also helps in building more modular and maintainable software. However, the overhead around setting up and maintaining a private NPM registry can be massive.
Using Bit, you can remove most of the overhead around a private registry while reducing the overhead around the packaging and publishing process.
In this short tutorial, I’ll show you how, using Bit, you can set up a private Node.js registry and publish dozens of components and modules in a just few minutes, in 3 steps.
Let’s get started.
Let’s set a private package registry for your team.
We’ll use Bit’s web platform to host the modules we share and the native NPM/Yarn client to install them.
First thing’s first, set up a registry.
a. Head over to bit.dev Click on get started.
b. Sign-Up. It’s free.
c. Create a collection:
To set a private collection, just select “private”. That’s it!
You now have a collection in Bit’s web platform, which also functions as a package registry. Let’s see how to publish packages to this registry.
Now let’s publish modules and components to our newly created registry. Since we set up the registry on Bit’s platform, we can leverage Bit for this workflow as well, to save precious time and effort.
First, install Bit. Then, head over to the project in which you have the packages you want to publish. Note that since we are using Bit, you can publish packages right from any existing project without refactoring.
#1 Install Bit
npm install bit-bin -g
#2 Create a local workspace for your project
$ cd project-directory
$ bit init
Instead of having to create a new repository, configure the package etc, let’s use Bit to isolate components and modules from existing projects and publish them as packages.
Let’s point Bit to the right packages in the project using the
bit add command.
Let’s track the components
logo in the following project’s directory structure.
│ ├── Button.js
│ ├── Button.spec.js
│ └── index.js
│ ├── Login.js
│ ├── Login.spec.js
│ └── index.js
5 directories, 13 files
To track these files as components we can use bit add with a glob pattern, pointing Bit to the path in which the modules we want to publish are found.
$ bit add src/components/*
tracking 3 new components
Note that Bit will automatically run through the module’s file & package dependancies, and create and isolate environment for the code which contains everything it needs in order to run in other projects.
Here’s a recommended example for React components.
$ bit import bit.envs/compilers/babel --compiler
$ bit import bit.envs/testers/mocha --tester
Now, let’s tag a version for the packages we are about to publish (following the previous example).
$ bit tag --all 1.0.0
3 components tagged | 3 added, 0 changed, 0 auto-tagged
added components: components/[email protected], components/[email protected], components/[email protected]
bit login to authenticate your machine to Bit’s platform.
$ bit login
Your browser has been opened to visit: http://bit.dev/bit-login?redirect_uri=http://localhost:8085...
Finally, export (publish) the packages.
$ bit export user-name.collection-name
exported 3 components to scope user-name.collection-name
All your packages will now be available in your collection, ready to install using NPM/Yarn in any project. Piece of cake, and we can use this workflow to quickly publish large numbers of packages in very little time.
Now that our packages are ready, let’s learn how to install them.
First, configure bit.dev as a scoped registry to your NPM client.
npm config set '@bit:registry' https://node.bit.dev
That’s it :)
Any package can now be installed using your native NPM/Yarn client.
Head over to the component/module page (Example).
Check out the pane on the top-right side. Choose the “NPM” tab and copy the command:
npm i @bit/user-name.collection-name.namespace.packagename
Let’s see an example.
Here’s a React Hero component shared as a package. Let’s use the following command to install it (user = bit, collection = movie-app, name space = components, package name = hero):
npm i @bit/bit.movie-app.components.hero
That’s it. You can now freely share and install these packages just as if you published them to any other NPM registry. The best
Another advantage of installing packages via Bit’s registry is that you can use Bit to import and make changes to a version of the actual source code of the packages right from any project you’re working on.
Unlike other registries, which require a cumbersome process for cloning and publishing changes to a package, Bit lets different team members import and modify packages from different projects.
For example, let’s look at this repo structure.
$ tree .
│ ├── hello-world.js
│ └── index.js
We’ll use Bit to import the left-pad component into your local project.
$ bit init
$ bit import bit.utils/string/left-pad --path src/left-pad
We can now make the required changes,
export them back to the collection (creating a new version) or to a new collection to share.
In this short tutorial we learned how to:
Sharing more code in a managed way, while reducing the overhead and time involved in this process, means your team can speed development and simplify the maintenance of your codebase.
Create your free account to unlock your custom reading experience.