How to write and publish your first NPM package
Tags: node, javascript, npm
Published: 2020-10-19 05:56:16



## Before we begin…
Before we begin this, you should have the following things set up.
- [GitHub account](https://github.com)
- [NodeJS](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) and npm installed
- [npm account](https://www.npmjs.com)

## Choosing a package name
![npm package name](https://dev-to-uploads.s3.amazonaws.com/i/9q3h68fuyk8e3crvn2q9.jpg)
You need to check whether the name is available for the npm package. If you are publishing unscoped (public) package; the name of the package should be unique.

However, if you are publishing a scoped (private) package then name does not have to be unique and name takes the format of **@npm_username/package-name**
Read more on
* [Scoped packages](https://docs.npmjs.com/using-npm/scope.html)
* [Package name guidelines](https://docs.npmjs.com/package-name-guidelines)

## Initializing the npm package
* Create a new directory and initialize using `npm init`
```
$ npm init
```
Initializing package will ask you a few questions for setup.
The default `package.json` file will be created in your project directory
```
{
  "name": "number-to-comma-format",
  "version": "1.0.0",
  "description": "Convert a number to comma separated format",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "RAJESH K",
  "license": "MIT"
}
```
Read more on [package.json](https://docs.npmjs.com/files/package.json)

## Create the Node module

The `main` field in package.json defines the entry point of your package.

Let's create `index.js` file
```
$ touch index.js
```
Add the following code to `index.js`

```
'use strict'

const defaultOptions = {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
};

/**
 * @param  {Number} num - Number to be converted
 * @returns - Formatted number format
 */
function formatNumberToComma(num) {
  return Number(num).toLocaleString('en', defaultOptions);
}

module.exports = formatNumberToComma;

```

## Add a README
It's a good idea to include documentation for your package so others know how to use it
Generally, a README should cover
* Description of what your package does
* Installation and usage instructions
* Example code
* Contribution guidelines
* License used by the package _[Choosing the right license](https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/licensing-a-repository)_

## Test your package locally
It is recommended to test your package locally before publishing to npm

* Create and initialize new project outside package directory
* Package can be installed by the following command
```
npm install number-to-comma-format
```
The problem with this is that your package is still not published yet so it isn't in npm. You need to reference your package locally while developing and testing it.
You could install the package using an absolute path to the package.
```
npm install /home/rajesh/dev/number-to-comma-format
```

## Initialize Git

Create a new repository in github for your package and push the source code to git
```
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/rajeshkumaravel/numbertocommaformat.git
git push origin master
```

## Publish the package to npm

* Sign in to npm
* You'll be prompted to enter username, password and email address which was used while registering to npm
```
npm login
```
* Now you can publish your package to npm by
```
npm publish
```

## Wrapping Up

VOILA...! Your package is now published on npm.
* Initialize `npm init`
* Add node module/source code
* Test your package locally
* git initialize
* Publish package `npm publish`

And that’s it!

I hope you found this article a useful primer for getting started with publishing on npm, and as always, thanks for reading!

_Check this out npm package for more reference [Node express request id](https://www.npmjs.com/package/node-express-req-id)  [Source](https://github.com/rajeshkumaravel/express-req-id)_

Happy Coding!
RK