Angular 2 – Publishing your Modules to npm.

Hi Friends,

Hope you are well. In this post let’s see how to publish your Ng2 Modules to npm. This post is a response to a question I saw on reddit. One of our friends Cyrille Tuzi has written an excellent article on how to achieve this and having learnt from him, I decided to show how to do it by actually making a module.

Let’s begin.

A video screencast of this post.

 

The entire code of the module is present here.

You might all remember about ngFone. It’s a simple angular library that I created long back using Angular 1 that would return the market name of a phone provided the model no (Which is what you would get programmatically). Let’s see how to migrate / create ng2-Fone which is simply the Angular 2 version of ngFone.

First of all let’s code the module.

Open up your terminal and give the below command.

Give the info (Just like filling out a form) and once the app is created open up the package.json and paste the below dependencies in it. Your package.json would look like this once done.

You could install these dependencies one by one as well, but this would be just quicker. Now simply run npm install to install all these dependencies.

Now create a new directory called src inside your project folder and create three files inside it namely index.ts, ng2-fone.ts and ng2-fone.module.ts

Open up ng2-fone.module.ts and add the below code.

Do not import BrowserModule here since this is going to be just a feature module that can be added to an angular app and this is no way going to function as an independent Angular 2 app.

Now open ng2-fone.ts and add the below code.