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
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.
Step 2
Please answer the following questions to create package.json
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.
-
-
- const calc = {
-
- add: function (num1, num2) {
- return num1 + num2
- },
-
- subtract: function (num1, num2) {
- return num1 - num2;
- },
-
- multiply: function (num1, num2) {
- return num1 * num2;
- },
-
- divide: function (num1, num2) {
- return num1 / num2
- }
- };
-
-
- 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:
- ## Introduction
- This module constaints basic functionality of calculater.
- ![Arvind build] (https:
- ![Arvind success] (https:
-
- Learn more to make your own badge.
- `<sheilds>` : <https:
-
- ### Installation
-
- Local module requires [Node.js](https:
- Install the dependencies and devDependencies and start the server.
- Install the local module by using command
- ```sh
- $ npm install @arvindbkushwaha/localmodule
- or
- $npm i @arvindbkushwaha/localmodule
- ```
-
- ## Usage
- First import the module into your application after installation
-
- ```
- import {calc} from ('./LocalModule.js');
- For Addition
- console.log("Addition:"+calc.add(5,4));
- For Subtraction
- console.log("Addition:"+calc.subtract(5,4));
- For Division
- console.log("Division:"+calc.divide(5,4));
- For Multiply
- console.log("Multiplication:"+calc.multiply(5,4))
- ```
- License
- ----
-
- 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.
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
Step 2
Open your command prompt and navigate to your project directory and type this command:
Provide the username, password, and email Id for authentication.
Step 3
Now to publish the module on npm, type the below command:
- 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.
Once it has completed successfully, it will upload our package on npm.
Now verify the package on the https://www.npmjs.com/ portal.
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.
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.
- npm i @arvindbkushwaha/localmodule
To include the module in our node.js application use require('@arvindbkushwaha/localmodule')
-
-
- const calObj = require('@arvindbkushwaha/localmodule');
-
-
-
- console.log("Addition: " + calObj.calc.add(5, 4));
- console.log("Subtract: " + calObj.calc.subtract(5, 4));
- console.log("Multiplication: " + calObj.calc.multiply(5, 4));
- console.log("Division: " + calObj.calc.divide(5, 4));
OutPut
To include the module in angular project use import {calc} from '@arvindbkushwaha/localmodule'
-
-
- import {calc} from '@arvindbkushwaha/localmodule';
-
-
-
- console.log("Addition: " + calc.add(5, 4));
- console.log("Subtract: " + calc.subtract(5, 4));
- console.log("Multiplication: " + calc.multiply(5, 4));
- console.log("Division: " + calc.divide(5, 4));
Output
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.
Now check the changes on the portal.