For javascript web developers like nodejs and angular developers, it's the most important and most useful npm modules. from npm modules, we can easily implement any modules as per our requirements very easily within 5 minutes.
if you are a web developer you can easily publish your own npm modules and help other developers to implement it.
So, let's start with how to publish our own npm nodejs module in npmjs.
For nodejs and angular, there are many different ways to create an npm library. Here we will discuss about make an module and publish it.
=>Requirements and overview of steps
->Sign up to npm (sign up in npm for free)
->Install Node & npm
->Setup nodejs project
->Creating the npm Package
->Test It locally before publish
->Login to npm
->Publishing the npm Package
->Republishing after some changes (if required)
->Install Node & npm
->Setup nodejs project
->Creating the npm Package
->Test It locally before publish
->Login to npm
->Publishing the npm Package
->Republishing after some changes (if required)
=>Setup nodejs project
mkdir welcome-test
cd welcome-test
npm init -y
Here you can create/give package name as per your module
=>Creating the npm Package
Create a file called index.js
This is the file which exicute first of all in package.
Now add your code here like as
//You can also import other node modules here and use it
const welcome = (name) => {
return "welcome to "+name;
}
module.exports = welcome;
=>Let's Test It locally before publish
sudo npm link
After running this command you can use this in any other projects without publishing in npm. now run this command and link it for testing purposes.
after this create your new nodejs demo project in another directory or you can use it in your existing project.
if you don't have any other project you can quickly set up a new nodejs project by the following command
Syntax:
mkdir <project name>
cd <project name>
npm init -y
Example:
mkdir welcome-test-demo
cd welcome-test-demo
npm init -y
for use, it from locally before npm publish run following command for npm install
Syntax:
npm link <package-name>
Example:
npm link welcome-test
After this import node module and use it as
app.js
const welcomeTest = require("welcome-test");
var welcomeText1 = welcomeTest.welcome("jayesh");
console.log(welcomeText1);
var welcomeText2 = welcomeTest.welcome("suresh");
console.log(welcomeText2);
var welcomeText3 = welcomeTest.welcome("paresh");
console.log(welcomeText3);
=>Login to npm
After successfully testing you can publish your package.
for that run following command for login npm in cmd
npm login
By this command input your user name, password, and email id as used in the npm signup
=>Publishing the npm Package
after login to npm, form cmd go to root directory and then run following command
npm publish
from this, your npm module will be published and you can check it from the npmjs link and search your module
=>Republishing after some changes (if required)
increase version number form package.json and then
npm publish
After those processes, you can see your module in npm. to display the proper installation process description you need to modify the README.md file. MD file has its own syntax to display in as per our requirements like as display heading, display example in code formate, etc. so if you don't know about it you can R & D and it's easy for learn it.
after updating the MD file you need to rebuild and republish it.
I hope it's clear how to publish your own node npm module in npm.
Thanks for Sharing this Information. Mean Stack Training in Gurgaon
ReplyDelete