Introduction 
In this blog, I’m going to share how to create npm packages for common functionality and deploy them to https://www.npmjs.com/package
Before we start, let’s set up the pre-requisites.
Step 1: Create an account in npmjs.com (https://www.npmjs.com/signup)

Step 2: Now navigate to the development machine and create a folder and prepare package.js and index.js files for a sample code snippet
Step 3: Log into npm using vs code terminal console
Step 4: Let us create an index.js file as a package source code. For testing purposes, will add very basic functions to calculate a circle, as below:
- /*
- JavaScript program to calculate the area and perimeter of a circle
- */
- // behaviour for our module
- function area(data){
- return Math.PI * data * data;
- }
- function perimeter(data){
- return 2*Math.PI * data;
- }
- // export to other modules
- exports.area = area;
- exports.perimeter = perimeter;
Step 6: Let's publish the above code using the below command
- npm publish
Step 7: Now let's verify the package in https://www.npmjs.com/ portal

If you wish to update the same package, simply change the version and run the same command
Its time to test our package in any client-side application. Copy the npm command from the portal as below:

Step 8: Install the package in our test application
Let us check in a project file
Let's include the package reference and call the test methods

Conclusion
We can create common functionalities like log file, collection filters, common calculations under certain npm packages and we can deploy them in NPM packages
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Rushi MehtaPosted Dec 16, 2019, 2:47 AM
Nice article
Chittaranjan SwainPosted Dec 15, 2019, 5:30 AM
Nice article.