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:
-
-
-
-
- function area(data){
- return Math.PI * data * data;
- }
-
- function perimeter(data){
- return 2*Math.PI * data;
- }
-
-
- exports.area = area;
- exports.perimeter = perimeter;
Step 5: Let's a have look at package.json file & create npm package for above code and publish it to npmjs
Step 6: Let's publish the above code using the below command
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