Releasing  your first NPM Package

Releasing your first NPM Package

Ever wondered how to create your own npm package, if so you are in the right place. In this article, I will give a walk-through for the creation of your first npm package. Before we jump right into coding, let's see what npm is.

Note that this article is based on my recent project of releasing a package. So you may make the necessary changes for the successful release of your package.

What is NPM?

  • npm is the package manager for Node.js. It was created in 2009 as an open-source project to help JavaScript developers easily share packaged modules of code.

  • npm is the command line client that allows developers to install and publish these packages that help in sharing open-source code for Node.js, front-end web apps, mobile apps, and robust, routers.

Publishing an NPM package may seem like a big task but let me tell you, it’s not that intimidating.So let's just start doing things, step by step we will just improve.So just stick around :)

Create a GitHub repo and local setup

First, let's start by making a GitHub repo. I will name it 'get-random-suggestions'. This is a recent package that I worked on. Locally create a folder and inside that create 2 folders, let the first one be ‘package’ and the second be ‘mock’.

Now clone the repo onto the package folder and set the repo URL as the remote origin. So we will have just a README.md file with get-random-suggestions as the title.

These are the steps :

Initialise the package folder with npm

Now type in :

npm init

So now it will ask a few questions as usual. You can give your data. Here I give the package name as get-random-suggestions. Description as Giving you random suggestions based on search parameters. Let the entry point be index.js.Git repo and keywords are default. You can add the author's name as your name.

Now just check the package.json which is generated, you can see that all the data is present there. Now we will create a file called index.js which is our entry point.

Creating the function and exporting them

In that index.js file, create a function called getRandomSuggestions with a parameter as the type of the data being sent. Now create another folder tour.json which is an array of objects, in which each object has 2 key-value pairs (here we consider adding the place name and its corresponding place.).Now import that to index.js and write the code like this. Note that we exported the function, this function will act as our package function.

// index.js
const tourData=require("./tour.json")

function getRandomSuggestions(type){
    let length,index,place;
    if(type==="tour"){
        length = tourData.length;
        index = Math.floor(Math.random() * length);
        place = tourData[index];
        return place;
    }
    else{
        return "Please set a valid parameter"
    }
}

module.exports = getRandomSuggestions

Now our folder will look like this.

Testing the package locally

Now the next step is to test the package locally for that we use the mock folder. To use our package in the mocks folder we need to first generate that package. So for that, we need to write the command :

npm link

After the link was created, go to the mocks folder and create a script.js file. Import that package into the script.js file.

// script.js
const suggest = require("get-random-suggestions")

But how do we get that package here? For that, we need to install our package that was generated locally. Now to locally install that package use the npm link <package_name> (here package name is get-random-suggestions).

npm link <package_name>

Thus we get that package here. Now just use the package and get the random data that you want to be based on the parameters that we pass in.

To run that file just run node <file_name>.

const suggest = require("get-random-suggestions")
console.log(suggest("tour"));
// output 
// {
// "name": "Manali",
// "place": "Himachal Pradesh"
// }

So we have written and tested our package let's enter the final step of publishing the package we have just created.

Publishing our package

Go to our package folder and type in:

npm publish

If there is an error, you need to create an npm account and verify your email as well. Again try to run the command and if successful. You are good to go. If you want to generate the package again after changes, we need to change the version of the package. Then, only this will work.

Now that we have done this just go to npmjs.com and search for the package name (here 'get-random-suggestions' ).

That's it we got our package published and ready to be used by the public. Make sure that push the code to the repo, and try to add a good README.

Conclusion

Publishing an npm package isn't that hard. The only thing you should consider is to write our function correctly and export it. You can play around with code and do many things that many developers try to achieve. I have just created a simple package called get-random-suggestions. You can contribute to the project by either adding some new JSON data like hotels, books, etc or adding new data to the existing ones.

That's it thanks for your time. If you find the blog informative and useful, please consider sharing and liking it. Also, keep an eye out for future blog posts related to Javascript and web development 😊.