In this tutorial, you will learn how to create and publish an NPM package.  NPM for those of you living under a rock, or who are new to JS is 'the' package manager for JavaScript.  One aim when writing code is to prevent duplication.  One way of doing this is to use existing modules.   Instead of having to write all these modules yourself, NPM allows you to hook into a huge library of packages from around the world.

When you start building an app it is very likely you will have some requirement and you can't find any existing NPM package.  If you have to write the code anyway...  why not built it within a module, publish and share it with the world?  Sound good, read on?


Create a new GIT Repo and Login To NPM

In Github create a new repository and clone it locally. 

Open a new terminal and ensure the current path is the same as your repo.  In the terminal, register your details like so:

Doing this will save your details within a file called ~/.npmrc. Doing this means you won’t have to re-enter them each time you create a new NPM package.  Next, log into NPM, using this command:

Create Your NPM Package

Next, you need to create a new NPM package.  Anyone who has been using JS should be familiar with this process.  Type, add in the project name, version, license etc..:

Next, we will  create a very simple function that returns a string 'helloworld', using this code within index.js:

Update the

Commit and push your code to GitHub

Next, commit your code and push it:

Publish Your NPM Package

After committing you can push your package to NPM so other people can access it:

Log into and you should now be able to see your module. Congrats! You’ve just published your first Node module.

Test out your module

Create a new folder on your PC. Run an npm unit to create a new app, adding in all your details. Next, import your package using:

In your Javascript.js file add some code like this:

That's it! If everything has gone correctly, you have now published and consumed your first NPM package. After you have published and updated a few times the process is fairly pain-free. Also, remember when you update your package, to up the version number!  If you want to see my first NPM package, you can see it over here: