How to create and publish npm package.
Tags: javascript, npm, node, programming
Published: 2022-09-04 10:58:40

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plt1udwikulnqvhi9i7s.png)



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

    }
    return "Invalid Number"
}
```

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
```

Before publishing our package we will first need to test it locally for that we run the command [npm link](https://docs.npmjs.com/cli/v8/commands/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvuu08a63mqr93jxm6mf.png)


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))
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5qinjgxnug8zayvp5sl.png)



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](https://www.npmjs.com/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7exio062pch3y44ks4ib.png)



#### 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.