DEV Community

Kajal kumari
Kajal kumari

Posted on

Publish your own NPM Package

What is npm?

npm stands for Node Package Manager, the default package manager for javascript's runtime node.js. It's the world's largest software registry where developers from every corner can use it to share and borrow packages. It allows javascript developers to share useful packages like Moment, Axios, babel, etc.

npm consists of two main parts :

  1. CLI (Command-line Interface) for publishing and downloading npm packages.
  2. An online repository for hosting javascript packages.

As a javascript developer many times we have downloaded and used packages from npm in our day-to-day life to make our work easier. Let's see how we can create one and publish it on the npm registry.

We will start with creating a new folder, I'm naming it as npm-package. Inside that let's create two folders package and test-folder. In the package folder, we will write code for the npm package, in the test folder, we will test our code locally before publishing it.

Image description

Now let's move inside the package folder,
cd package/
In parallel, we will create a new repository on GitHub and follow the instructions for git init locally.

Image description

Copy the commands and paste it on the terminal and it will initialize and create a basic readme file for us.
Now we will do npm init and provide the information about what our package will do, its name, version, description, entry point, git repository, keyword, author, dependencies, etc.

In our case, we will create a package in which we will pass a word and that package will return synonyms and antonyms for that word. Navigate to the package folder and create a file named index.js. Here we will write code for our package, and write a basic function that will return all the synonyms and antonyms of the provided word, To get the synonyms and antonym we will use a free API https://api.api-ninjas.com/v1/thesaurus and will fetch data from that using Axios.

const axios = require('axios');

async function fetchData(word) {
    const endpoint =`https://api.api-ninjas.com/v1/thesaurus?word=${word}`;
    try{
        const response = await axios.get(
            endpoint,
          {
            headers: {
              "X-Api-Key": <API KEY TOKEN>,
            },
          }
        );

        const data = await response.JSON();
        return data;
    } catch(err) {
        console.log("err::: ", err);
        return null;
    }
}

function findSynonymAntonym(string){
    fetchData(string).then(data => {
        if(data) {
            console.log("data", data)
        }
    })
}

module.exports = findSynonymAntonym;
Enter fullscreen mode Exit fullscreen mode

Now, we will link this folder so that if we try to install the package it will link from the package folder location. For that type a command in the terminal

npm link
Enter fullscreen mode Exit fullscreen mode

And then switch to the test folder. Here create a file script.js and in that, we will test our package. Import the package and call the function with the word.

// importing the package
const findSynonymAntonym = require('synonym-antonym-generator');

//utilizing it.
console.log(findSynonymAntonym("bright"));
Enter fullscreen mode Exit fullscreen mode

Now, we will link this folder with the package folder and then test this before we finally push and publish it online

npm link synonym-antonym-generator // <your package name>
Enter fullscreen mode Exit fullscreen mode

This will locally create a node module folder and inside it will install our package in the test folder. Now let's run the code to test the package. Write in terminal

node script.js
Enter fullscreen mode Exit fullscreen mode

this will give all the synonyms and antonyms of the word we provided, the result will look like something similar to

{
  "word": "bright",
  "synonyms": [
    "shining",
    "luminous",
    "dazzling",
    "glowing",
    "shiny",
    "radiant",
    "brilliant", ...
  ],
  "antonyms": [
    "dim",
    "dull",
    "dark",
    "gloomy",
    "lackluster",
    "darkened", ....
  ]
}
Enter fullscreen mode Exit fullscreen mode

We tested our code we wrote our code now is time to publish the code let's go back to our package folder where the package exists and in the terminal type npm login to log into the npm account, if you don't have npm account you can go to npm and create one account.
signup
When we do npm login it will ask for our username and password and email. Type that and we will be logged in to our particular username.

Image description

After that, the last step will be to publish our package. For that simply write this and wait for this to run.

npm publish
Enter fullscreen mode Exit fullscreen mode

Once its successful we will go to the npm website and search for our newly created npm package "synonym-antonym-generator".
and Hurrayy!!! We can see our newly created package.

Image description

Summary

  1. Publishing the npm package is quite easy you just need to know a few commands
  2. For testing purposes we first linked the package and the test folder using npm link
  3. Simply create an account on npmjs.com and then login locally in the terminal using npm login command fill in the basic things and run the command npm publish.

I hope you liked reading this and learned something new today. Thank you for staying up here until the next blog. Bbye!

Happy learning! Cheers 🥂.

Top comments (0)