How to create your own npm packages?

 Two things to keep in mind:

1. Have a package.json file, take a note on the entry point (you can keep it default for now, index.js)

2. Keep track of the version number in package.json file (by default it is 1.0.0, keep it as is for now)

Creating a package

Follow the below steps to create an npm package 

1.       Create a folder:  array-duplicate-reducer

2.       Run : npm init

3.       Leave the defaults as-is for now. In author, you can give details in the below format:

kavyashree <bjkavyashree@gmail.com> (javascripthisday.blogspot.com)

 your-name <email ID in angle bracket> (website URL in round bracket)

·         This would have created a package.json file by default.

·         You need to add an index.js file which is the first file to be executed when the package loads.

·         I added the below code:



4.       Run npm adduser


5.       npm publish --access=public

And that’s it!

Note: You will need to confirm you email address if you are doing this for the first time for this to work!

Login to https://www.npmjs.com/ and check if your package is added like below:



Using the package

1.       Create a new folder and run npm init inside it

2.       Run npm install array-duplicate-reducer

3.       Create index.js file and paste the below:


4.       Open command prompt in the same folder containing index.js file and run

node index.js

This should give the output as below:


Comments