How to Build Own NPM package & Publish

NOTE : This article is based on what i learned from setting it up .
What is NPM
npm is a package manager for the JavaScript programming language. It is the Default package manger of node.js and it was created in 2009 as an open source. It will help to js developer to easily share code when developing (like an example when we need to implement dropzone in our web application there are thousand of pre-build node packages we can find and choose one and implement easily with out doing from the scratch). This article will teach you how to build your own package and publish it to npm.
What you Need ?
all you need is npm command line tool which goes by npm. npm is distributed by node.js that mean when you install node.js automatically npm got installed. here is link for install node.js click here
Here we go !
open your terminal and make directory using your package-name
change the directory to the project directory
Initialize NPM package
note : when we used common name for the package and if it already in npm when we publish it show package already exists. use unique and meaningful name
NPM init command will ask several question about package for create a package.json in the end package.json file will automaticaly create in you directory and you can check all the details are correct.

Package.json is the single and most important file as far as creating/publishing an NPM package is concerned, without it, you won’t be able to publish whatever you create to the NPM registry.
package.json main field will refer the name of your package that would be loaded when your package require by another package by default it point to index.js.
If you are writing a small library, you can put all your code into index.js
. But more often, we will abstract our code and put it into separate files. So the ideal approach is to keep all your source code in src
dir.
This is the most widely used and recommended setup for source code nowadays, although it varies from one library to other.

Put Our Script into index.js
create index.js file and put your script and in the end this code with first method you want to excute


Publish Package
You need to create and account in the npm registry from going to this link click here after create account go back to terminal and use command npm login

after fill the username ,password and email and you will log in to your account in cli
you can check is that your correct account by typing “npm whoami”
Now publish your package using below command

when you see the above screen it mean your package published on your account if you see error message it mean package not published please check the process again and find the error.
you can check your package by login in to your account
https://www.npmjs.com/~{username}/{package-name}
Time To Test
To test the package you can simply need install and use it
create directory and go to that directory

install your package using npm install

execute file using node ./yourfilename.js you should see the result.

Happy Coding ! ❤