Hello Developers , in this article you will learn how to publish npm packages .

In this article we will first build a pretty simple npm package and then we will publish it to npm . The process of publishing npm package will be applicable to alomost every package you are going to build.

In our package we are going to make a function , this function will take an integer input and output it’s factorial .

Make Your Account on npm and Login

So guys first of all you need to make an account on npmjs.com and verify it with your email address , without verification you won’t be able to push any package on npm .

Now in your command line type the below command

npm login

A prompt asking for your email , username and password will open , enter your correct credentials in it.

Initialize Your Repository

Now it’s time to initialize your package directory with the below command

npm init

The above command will ask you some questions for your package you can anwer or just press Enter for default value .

  • Package Name: This must be unique (Check the NPM website to see if your name is taken)
  • Version: 1.0.0
  • Description: Make it descriptive so that user can understand the purpose of this package
  • Entry Point: index.js (you can change it )
  • Test Command: Command for testing your package , you can leave this blank by pressing Enter
  • GitHub Repository: If you have pushed your package to git repository then provide the repository link here .
  • Keywords: Keywords are helpful in searching for your package .
  • Author: Your name goes here
  • License: ISC (A license for open-source software)

and then will create a package.json file in your package directory containing your package info .

Create Entry File of package

Now make a file with the name of your package entry point , for this package entry point is index.js . In this file we define our functions for usage.

We will use module.exports and set it’s value to a function which will accept the integer , apply the logic of factorial using recursion and return the result .

module.exports = function fact(n) {
     return n *fact(n-1);
}

module.exports is used to export a function so that another file can import using require() function and make use of it .

Publish The Package

Now it’s time to publish our npm package using the below command

npm publish

Now if you login to your account on npmjs.com and go to packages section , you will see your published package name there.

Add Readme.md File

You must have seen a brief documentation of every package on npm which make it easier for other developers to understand the working of that particular package .

To make a documentation for your package you need to create a Readme.md in file your package directory containing the brief description of your package’s working .

You can see the guidlines of markdown syntax here

Update Version and Publish

After adding the Readme.md file , you need to update your package’s version before publishing it again on npm . Change the version to 1.0.1 in package.json file

Then after run the below command to publish

npm publish

Your new update will publish in just few seconds on npm .

If you want to unpublish your package from npm then you can simply use the below command

npm unpublish <the name of your package> -f

And now if you check in your packages , the package will be removed . So that’s all for this article…

You can follow me on Linkedin : Anand Raj

Don’t forget to follow our Linkedin page for latest updates : BePractical

Prev Article : how-to-build-your-own-library-with-vanilla-javascript-part-1/

Thank you for reading ✌