How To Create And Publish An NPM Package

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?

Pre-requisites

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:

npm set init.author.name "Jon D Jones"
npm set init.author.email "code@jondjones.com"
npm set init.author.url "http://www.jondjones.com"

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:

npm login

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..:

npm init

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

const sayHello = function() {
    return "Hello World";
};

module.exports.helloWorld  = { sayHello  };

Update the README.md

Commit and push your code to GitHub

Next, commit your code and push it:

git add .
git commit -m “Initial release”
git tag 1.0.0
git push origin master --tags

Publish Your NPM Package

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

npm publish

Log into https://www.npmjs.com 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:

npm -i <package-name>

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

var helloWorld = require('project-name').helloWorld;
console.log(helloWorld .sayHello);

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: https://github.com/jondjones/js-number-formatter

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top