DEV Community

Hamza Ali
Hamza Ali

Posted on

How to create and publish npm package.

Creating npm package

Creating your npm package may sound difficult to do but it is surprisingly easy. In this blog post we are going to learn how we can create and publish npm package.

Let’s create a folder named square-number you can name your folder whatever you want. Now inside square-number directory run the command npm init -y this will create a package.json file if you want to customize the values in pacakge.json then just run the command npm init and then answer the question that are asked after running the command.

Description of npm package

So for this bolg we are going to create a simple npm package which will only return us the square of a number and if a value other then number is passed, it will return a message saying “Invalid Number“.

Lets code

First we will create a index.js file inside our square-number folder. And inside that folder we are going to simply write a function which will satisfy what we described above.

ss

function square(number) {
    if(typeof number === 'number') {
        return number * number

    }
    return "Invalid Number"
}
Enter fullscreen mode Exit fullscreen mode

The above function is going to be defined in our index.js file. What it does is it take a value and check if the value is of type number if it is then it returns the square of number otherwise if the value is not a number it returns a message saying “Invalid Number”.

This is going to be a node module so we are going to export is from the index.js file by adding this below code at the bottom of our index file

module.exports = square
Enter fullscreen mode Exit fullscreen mode

Before publishing our package we will first need to test it locally for that we run the command npm link

Testing

To test our package we will create a new folder named test. And inside that folder we will test our package by passing the number, string and undefined to it.

First run the command npm link square-number inside the test folder. What this will do is install the package inside test.

After running the command you will notice that a node_modules folder is created in the test folder and inside that node_modules you will find our square-number.

ss

Now to test we will create a index.js file and we will add the below code inside it.

const squareNumber = require('square-number');

console.log(squareNumber(2))
console.log(squareNumber("2"))
console.log(squareNumber(undefined))
Enter fullscreen mode Exit fullscreen mode

At first line we are importing our package and next we are calling the package and passing it the number 2 and on the next line we are passing a string and then we are passing the value of undefined to test if all of our use cases are handled or not. Now lets run our index.js file by running the command node index.js .

ss

You can see that the first call to our package return the square of number 2 and the second and third call returns a message saying “Invalid Number “ because we have passed a string and undefined to it.

Lets now publish our package

To publish we will first need to create npm account. After creating the account we will sing in from our terminal by running the command npm login This will ask you to enter you account credentials.

The next step is to run the command npm publish and this command will publish our package to npm server.

ss

Note

Sometime the name of a package is already taken so to avoid that you might need to create a named space package by just altering the package.json file and change the name of package by adding a prefix to it which should have @ at the start of it @hat52/{name of the package} . To publish the named package you will need to run the command npm publish --access=public what this does is it make the package public because by default name space packages are private.

Conclusion

We have created a simple package which returns the square of the number passed to it. This is just to give context of how we can create a npm package. If you want to go into details of how we can create a complex package . Follow me because I am thinking of creating a time picker package in the near future.

Thanks for reading and if you have any question or suggestion leave them in the comments.

Top comments (0)