Once Upon a Library (2/3)

Rafael Trindade Chiappetta
Dev Friends
Published in
4 min readApr 7, 2019

Building, Packaging, and Publishing your Library

Previously on Once Upon a Library

In the first part of this series, we created an Angular 7 library named in a format like “@myscope/my-library”, which in my case was “@devfriends/df-library” and this is what I’ll be using as an example.

In this chapter

Now, you are going to learn how to build a distribution of this library, package it into a single file, and publish it to the npm repository or to a local repository of your choice. We chose Nexus Repository OSS.

Building Your Library

Building your library is easy as running the command, preferably from your root’s directory:

ng build @devfriends/df-library

Once you do this, a directory will be created under dist folder with the following structure:

Library distribution folder

Packaging Your Library

Move to your library’s distribution’s folder, in my case dist/devfriends/df-library, and run the command:

npm pack

After you do this, you will see that a tgz file, named after your library’s name and version, appeared in your directory. It is recommended that you always run the packaging command in order to keep track of your builds’ files.

Distribution package file

Congratulations! Your library is now ready to be published.

Publishing Your Library into npmjs Repository

In order to publish you library into npmjs repo, you will need to sign up for an account on their website.

After creating your account, you need to tell your local installation of npm who you are by running the command and login with your npm user, password, and e-mail:

npm login

This will add your credentials to your ~/.npmrc file.

Once you are logged in, you can publish your library by moving into you dist folder (where the package file is located) and running the command:

npm publish dist/devfriends/df-library/dev-friends-df-library-0.0.1.tgz

Since devfriends is a private/closed scope, you should use your own scope in order to be able to publish to npm.

If it complains you must sign up for private packages, try changing your library’s scope, or make it a public library by adding the — access public option to your command:

npm publish dist/devfriends/df-library/dev-friends-df-library-0.0.1.tgz --access public

You library is now ready to be installed in any project.

Publishing Your Library to a Local Repository

If you don’t want your library to be published in npmjs public repository, you can signup for private repos or install your own repository manager.

In this section we are going to use Nexus repository manager for storing our library.

Installing Nexus

Getting started with Nexus is very easy, all you need to do is:

After installation, Nexus will be available at: localhost:8081

The default username and password are admin | admin123. You should change this the first time you log in.

Enabling npm repo on Nexus

In order to publish npm packages to your local repository, you have to setup a npm repo first. So:

  • Access nexus repo at http://localhost:8081
  • Log in as admin;
  • Click on the gear icon in the top navigation bar;
  • On the left menu, click on repositories;

There are 3 types of repositories you need to create

  1. Proxy repo: this will point to the npmjs repo;
  2. Hosted repo: this is you local repository;
  3. Grouped repo: this will put the 2 previous type together.

Create your proxy repository:

  • Click on the Create Repository button;
  • Select npm (proxy) and create a repo just like the image:

Create your hosted repository:

  • Click on the Create Repository button;
  • Select npm (hosted) and create a repo just like the image:

Create your grouped repository:

  • Click on the Create Repository button;
  • Select npm (group) and create a repo just like the image:

Enabling Authentication

  • Go to the Security tab and click on Realms.
  • Activate the npm Bearer Token Realm.

Publishing Your Library into Your Nexus Repository

Just like publishing to npm registry, you will also need to authenticate into your local repository. In order to do so, run the command:

npm login --registry=http://localhost:8081/repository/npm-local/ --scope=@devfriend

Provide username, password, and e-mail. You can use the admin user, but you should definitely create a user for yourself.

Once you are signed in, run the publish command:

npm publish dist/devfriends/devfriends-df-library.0.0.1.tgz --registry http://localhost:8081/repository/npm-local/

You should now see your library in you local repository:

You library, published to your local repo

Congratulations! You have setup your first local repository and published your first library into it! You rock!

… To Be Continued …

Once Upon a Library (3/3): Installing you library into other project.

--

--