How to build your Own NPM package

Publicerades den - Senast redigerad

We've seen the variety of JavaScript package managers in the JavaScript Package Management article. 

From there we saw how useful the Node Package Manager was with the way it helped developers publish, manage, and reuse JavaScript packages. In this article, we will go through the steps involved in creating and publishing a new NPM package to the NPM registry, which is a collection of open source code modules which can be downloaded and used by anyone.

Note: In case we are not allowed to publish the source code of the project we are working on because of NDA agreements, the traditional NPM packaging is NOT an option for us. We could consider the npmjs.com private module repos, but this is a paid service.

NPM goes hand in hand with Node.js so we have to install node to create a new npm package. For Ubuntu, we need to run:

    sudo apt-get install nodejs

When we install nodejs, npm comes with it. After installation, you should check the versions. It has to be the latest revision for creating and publishing packages. At the time of writing, the npm version is 2.10.1 and nodejs is 0.12.4, so in case you have an older version of Ubuntu, you can find how to install the latest node and npm on this askubuntu link.

node version

Now let's start creating a new npm module. First we're going to need a public git repository, so if you don't have one you can easily create one on GitHub or BitBucket. I named the repo js-random-string and this will also be the name of the npm module.

github js-random-string

For the next step let's create a directory with the name of the package and execute npm init inside.

This command will guide us through the process of creating a new module by asking for all relevant information, as well as the location of the source code (the git repo).

npm init output

The first question is related to the name of the package, which defaults to the directory name. The second is the version; npm supports a so-called semantic version of packages, so we should update the default value of 1.0.0 to 0.0.1 since this is the initial version of the package. The description field is self-explanatory. The entry point is the file that the package manager is going to load--the default value is index.js and we should not change that. The author and license fields should be obvious. After we provide all the information, the npm generates the content of packages.json file.

This file is the main descriptor of our package. npm displays the content and asks for a confirmation before creating the file:

packages.json content

The next step involves cloning the git repository. Copy the packages.json file there, create the index.js file, and add everything to git.  

	git clone https://github.com/gergob/random-string 

After doing all the steps above, the package folder should have the following structure:

greg@earth:~/Development/_freelancer/npm_packages$ tree random-string/
random-string/
├── index.js
├── LICENSE
├── package.json
└── README.md

0 directories, 4 files

The implementation of the module (index.js):

function getRandomString (stringLength) {
	var myStrings = [
			'hello',
			'world',
			'going',
			'home',
			'with',
			'and',
			'eating',
			'food',
			'drink',
			'cola',
			'orange',
			'melon',
			'dragon',
			'chicken',
			'walking',
			'road',
			'city'];

	var result = '';

	for (var i = 0; i< stringLength; i++) {
		var index = Math.floor((Math.random()*100)) % myStrings.length;
		
		result += ((myStrings[index]) + ' ');
	}

	return result;
}

exports.getRandomString = getRandomString;

To publish the package, you're going to need an npm registry account. If you don't have one, we can create one with npm adduser and set up the account details locally after running the command. It will then ask for user name, password and email address.

Once this is done, we can validate if the new user config is correct by running npm whoami, which should display our username. To run the uploaded package, we need to run npm publish. If this executes successfully, our new package will appear on the npmjs.com website in a few seconds. We can then access it with the URL: https://www.npmjs.com/packages/js-random-string

uploaded npm package

If we want to use our new module in a project, all we need to do is execute:

    npm install js-random-string

 

Upplagt 11 juni, 2015

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Nästa artikel

What to Do When Your Online Accounts Get Hacked