Publish Module to NPM (Node Package Manager)

Introduction 

 
This article will help you to create, publish, and re-publish our own module to NPM(Node package manager) along with readme.md files.
 
It contains the following parts:
  • Create our own module with a readme.md file
  • Publish module
  • Use module in our project
  • Republish module 
Why we publish our own package:
  • The package enables the developer to simply use functionalities without handling any complexities.
  •  It is a great way to demonstrate your JavaScript and programming skills
If you haven't checked my previous article on node Node.js Module With Examples. I recommend you to go through it at least once.
 
To create and publish a node module, we require node.js. If you haven't installed it then please download and install it from node.js.
 
Once the installation of node.js completed, please follow the below step to create our own module from scratch or refer to my previous article.
 

Create our own module

 
Step 1
 
Open the command prompt and type the below command.
  1. npm init  
Step 2
 
Please answer the following questions to create package.json
 
 Publish Module To NPM(Node Package Manager)
 
 Step 3
 
 Now create an index.js file and paste the below code in it.
 
 Type the below command to create a blank index.js file.
  1. type Nul > index.js    
  1. // create a variable calc that have four function add, subtract, multiply and divide.  
  2. // https://shields.io/ To add sheilds in readme.md  
  3. const calc = {  
  4.     // Add the two number  
  5.     add: function (num1, num2) {  
  6.         return num1 + num2  
  7.     },  
  8.     // Subtract the two number  
  9.     subtract: function (num1, num2) {  
  10.         return num1 - num2;  
  11.     },  
  12.     // multiply the two number  
  13.     multiply: function (num1, num2) {  
  14.         return num1 * num2;  
  15.     },  
  16.     // divide the two number  
  17.     divide: function (num1, num2) {  
  18.         return num1 / num2  
  19.     }  
  20. };  
  21.   
  22. // export the modules to consume on different modules.  
  23. module.exports.calc = calc;  
 Step 4
 
 This step is purely optional but if added along with our module then it will help others to install and use your module in a better way.
 
 Create a readme.md file on the root folder and paste the below code:
  1. ## Introduction  
  2. This module constaints basic functionality of calculater.    
  3. ![Arvind build] (https://img.shields.io/badge/arvind-build-blue?style=flat&logo=appveyor)  
  4. ![Arvind success] (https://img.shields.io/badge/arvind-success-green?style=flat&logo=appveyor)    
  5.   
  6. Learn more to make your own badge.  
  7. `<sheilds>` : <https://shields.io/>    
  8.  
  9. ### Installation  
  10.   
  11. Local module requires [Node.js](https://nodejs.org/) v4+ to run.  
  12. Install the dependencies and devDependencies and start the server.  
  13. Install the local module by using command  
  14. ```sh  
  15. $ npm install @arvindbkushwaha/localmodule  
  16. or   
  17. $npm i @arvindbkushwaha/localmodule  
  18. ```  
  19.  
  20. ## Usage  
  21. First import the module into your application after installation  
  22.   
  23. ```  
  24. import {calc} from ('./LocalModule.js');  
  25. For Addition  
  26. console.log("Addition:"+calc.add(5,4));  
  27. For Subtraction  
  28. console.log("Addition:"+calc.subtract(5,4));  
  29. For Division  
  30. console.log("Division:"+calc.divide(5,4));  
  31. For Multiply  
  32. console.log("Multiplication:"+calc.multiply(5,4))  
  33. ```  
  34. License  
  35. ----  
  36.   
  37. MIT  
To read more about how to write readme.md file, then please refer the article https://guides.github.com/features/mastering-markdown/
 
To learn more about how to create and use your own badge, then please refer the article https://shields.io/
 
I have created my badge from the above article and used it in this way into the readme.md file.
  1. ![Arvind build] (https://img.shields.io/badge/arvind-build-blue?style=flat&logo=appveyor)    

Publish Module

 
To publish our own module, we required an account in the NPM registry. So if you do not have an account, then create it through this link https://www.npmjs.com/signup
 
Step 1 
 
Publish Module To NPM(Node Package Manager)
 
Step 2 
 
Open your command prompt and navigate to your project directory and type this command:
  1. npm login  
Provide the username, password, and email Id for authentication.
 
Publish Module To NPM(Node Package Manager)
 
Step 3
 
Now to publish the module on npm, type the below command:
  1. npm publish --access=public   
Note
I had modified the package name in package.json to '@arvindbkushwaha/localmodue' from 'localmodule' before publishing as the same module name already exists. 
 
Publish Module To NPM(Node Package Manager) 
 
Once it has completed successfully, it will upload our package on npm. 
 
Now verify the package on the https://www.npmjs.com/  portal.
 
Publish Module To NPM(Node Package Manager) 
 
This is the advantage of having proper in the readme.md file in your module. As it gives a brief description of the package, installation, and uses of your package. 
 
Publish Module To NPM(Node Package Manager)
 

 Use Module in our project

 
 To use the module in our project, copy the command from our readme.md file or from the npm portal.
  1. npm i @arvindbkushwaha/localmodule  
 Publish Module To NPM(Node Package Manager)
 
 To include the module in our node.js application use require('@arvindbkushwaha/localmodule')
  1. // import the local module in your project 
  2.   
  3. const calObj = require('@arvindbkushwaha/localmodule');  
  4.   
  5. // To call the all fuction simply use in this way  
  6.   
  7. console.log("Addition: " + calObj.calc.add(5, 4));  
  8. console.log("Subtract: " + calObj.calc.subtract(5, 4));  
  9. console.log("Multiplication: " + calObj.calc.multiply(5, 4));  
  10. console.log("Division: " + calObj.calc.divide(5, 4));  
 OutPut
 
 Publish Module To NPM(Node Package Manager) 
 
 To include the module in angular project use import {calc} from '@arvindbkushwaha/localmodule'
  1. // import the local module in your project  
  2.   
  3. import {calc} from '@arvindbkushwaha/localmodule';  
  4.   
  5. // To call the all fuction simply use in this way  
  6.   
  7. console.log("Addition: " + calc.add(5, 4));  
  8. console.log("Subtract: " + calc.subtract(5, 4));  
  9. console.log("Multiplication: " + calc.multiply(5, 4));  
  10. console.log("Division: " + calc.divide(5, 4));  
 Output
 
 Publish Module To NPM(Node Package Manager) 
 

Re-Publish Module

 
If we modified the code or readme.md file in our existing module and wish to have these changes in our package, then we have to republish the package.
 
To publish the existing package, simply change the version in our package.json and run the same command.
 
 Publish Module To NPM(Node Package Manager)
 
 Now check the changes on the portal.
 
 Publish Module To NPM(Node Package Manager) 


Similar Articles