How to create and publish npm modules for Nodejs applications

This tutorial is mainly catered for Windows users. If you’re feeling lazy and you just want a code example, visit my Github repository

For creating and publishing an npm module, you will need two things:

  1. Nodejs – npm and node are automatically added into command-line path upon installation
  2. Github – Account needed to publish npm module, installation needed to ease syncing

My first npm module was published with the help of Brent Ertz’s web tutorial, so do refer to it for a more detailed explanation with the addition of testing using chai & mocha frameworks, which will not be discussed here.

Firstly, in your command prompt, configure npm.

>npm set init.author.name “Roy Lee”
>npm set init.author.email “roy.lee.2013@sis.smu.edu.sg”
>npm adduser

Create a folder in Documents/GitHub. This will be the name of your module and where your module will reside in. Make sure the name does not clash with any name in the existing npm registry. (You can check this by searching for the name of a module on the npm website)

git folder

Add a simple js file with the following syntax. (The exports attribute is a significant feature of js which allows functions to be reused, especially in the case of an npm module)

jsfile

The function “getAll” in the image is a wrapper around the cloudinary api. For the purpose of this tutorial, you may add a function like this one below (It’ll be easy to test as well)

consolePrint: function(){

console.log(“My first npm module”);

}

Save your mini module, and commit and publish into your repository on Github. Any further changes can be done by commiting and syncing to Github.

Noticed the large amount of other files such as package.json in my earlier sublime text screenshot? no worries, these files can be automatically generated for you.

Next up, in the command line, change directory to within the folder you created, and type

>npm init

Fill in the required fields as prompted, and a package.json file should turn out like this

packagejson

The following instructions by Brent Ertz will prove really useful.

In the command line, make sure you are in the root directory of your package and enter these commands

1. Make sure package installs properly, by installing your package globally.

> npm install . -g

2. Check to see if the package exists.

> npm ls -g

If the two commands were successful,

> npm publish

Any further changes can be done by

1. Commiting and syncing to github
2. >npm version 1.0.x (where you enter your version number before publishing)
3. >npm publish

Test your module by installing the normal nodejs way

>npm install module-name

Thanks a bunch for reading, have fun publishing!

Do leave a comment if you have any queries, will be glad to help 🙂

Advertisements

2 thoughts on “How to create and publish npm modules for Nodejs applications

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s