loading...
Cover image for How to write and publish your first NPM package

How to write and publish your first NPM package

rajeshkumaravel profile image Rajesh Kumaravel Updated on ・3 min read

Before we begin…

Before we begin this, you should have the following things set up.

Choosing a package name

npm package name
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

Initializing the npm package

  • Create a new directory and initialize using npm init
$ npm init
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

Read more on 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
Enter fullscreen mode Exit fullscreen mode

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;

Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Now you can publish your package to npm by
npm publish
Enter fullscreen mode Exit fullscreen mode

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 Source

Happy Coding!
RK

Discussion

pic
Editor guide