How to create your own NPM module ?


Introduction

As we know npm has one of the largest collections of packages for almost everything you require for your project. it has become possible due to lots of collective efforts of technology enthusiastic people. if you are one of them then you are in right place.
Well, the answer is npm is open for the public to upload packages, and whatever packages we get to see in the npm directory all those are uploaded by individual contributors or organizations. Sometimes npm packages allow the free trial to make use and if you want to continue using it you will have to pay. Sometimes developers provide packages for free but if you want to appreciate their work you can help them by "petrion" if they have provided a link.

Publish NPM Module

In order to start creating a package, you must have your npm account its very simple to create. once you are done with account creation go through the following steps.

1. Create Npm Account and log in.

 if you have an npm account then login it. else create an npm account it's completely free
it just requires few details to create an account.


2. Create Package.

In the local machine create one new directory for the package.

note: check if a package with the same name already exists in the directory. you cannot use duplicate package names  
  mkdir your_package_name
  cd your_package_name
  npm init -y


3.Write logic

Now create index.js or server.js whatever filename you have given as the main file in package.json ( by default it’s index.js) and write your simple code.
 
   console.log("my first module");
     


4.Publish

open your terminal/shell/cmd and change the directory up to your package. and login to the npm account.
 
   npm login
     

it will ask few details like email, username, password just complete that and now you are logged in. after login now its time to publish it.
 
   npm publish
     

this command will finally publish your package will take some time to list in the npm directory check it after some time. but if you want to verify it you can check it in your profile.


Conclusion 

we learned about how to publish our own npm package.

Post a Comment

0 Comments