How to publish your project to npm?
Tags: javascript, node, tutorial, opensource
Published: 2022-11-27 22:58:41

## Hello everyone!

Today, I created the first release for my [SSGulnur tool](https://github.com/gulyapulya/SSGulnur). 

### Releases 🚀

As you are building your tool, it is alright to store it on the GitHub and apply it by cloning the repo, building and running it locally following the pre-requisites and instructions outlined in the README file. 

However, when it comes to the actual users, not everyone knows how to do it, so it is better to provide releases which are easy to understand, download and use.  

For that, I am going to publish my project to the registry so that it can be installed by name. 

### npm 🗃

As my project is using node.js, my obvious choice of release and package manager was [npm](https://www.npmjs.com).  

### Approach 👩🏻‍🏫

It does not require much to just publish a package. You can do it by only running the `npm publish` command, however it will be missing some of the industry standard procedures. 

For example, it will not ensure that you are using [Semantic versioning (Semver for short)](https://semver.org) or associate a git tag for each update. 

> If you do not know what Semver is, I have a brief explanation at the end of the post.

Therefore, we will be using a tool called [np](https://github.com/sindresorhus/np), which will 

1. run the tests (if there are any)
2. update version in package.json according to Semver
3. create a git tag according to Semver
4. push the package to Github
5. push the package to npm
6. create release notes for every update

all in one command. 

### Steps 📜

In order to use **np** there are a few requirements (which you most probably have already met):
1. Project needs to be a Git repository: 
   * It needs to have a remote.
   * You must have pushed to the remote at least once.
   * You also need to make sure your working directory is clean.
   * You [set up ssh authentication keys](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
2. You need to have an [npm account](https://www.npmjs.com/signup)

All you have to do is:
1. Navigate to your projects folder and open a terminal.
2. Login to your npm account using `npm login` 
3. Install np globally `npm install --global np`
4. Run `np` and follow the prompt

![semver](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4t8d3olnr88z0j5ol2l.png)

![process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4a8mt1450fkaninn2nv.png)

5. Optional: add script in package.json for releases using np.
```
"scripts": {
    ...
    "release": "np"
  },
```

### Tadaaa 🎉

Now, you should have an npm package as well as associated tags and releases on GitHub. 

So, my tool can be installed by only using `npm i ssgulnur`.

My resulting npm package: 
https://www.npmjs.com/package/ssgulnur

GitHub releases:
https://github.com/gulyapulya/SSGulnur/releases

![TADA](https://1b-f.s3.eu-west-1.amazonaws.com/a/34219-55525652-03C7-4F99-9540-65418205EAFE-0-1520001959.gif)

### Semver 💡

Now, lets talk a little bit about the versioning. [Semver](https://semver.org) is a simple set of rules and requirements that dictate how version numbers are assigned and incremented. 

The ideas is that three numbers are used to explain the version and one of them gets incremented depending on how big the change has been.

So the version is structured as MAJOR.MINOR.PATCH and you increase:
- MAJOR version when you make incompatible changes (some previous logic cannot be used anymore)
- MINOR version when you add functionality in a backwards compatible manner (new features that do not break the previous logic)
- PATCH version when you make backwards compatible bug fixes