How to Build Own NPM package & Publish

Hasitha Charaka 🔵
4 min readMay 30, 2020

node package manager

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.

npm init

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.

package.json

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

index.js
directory view

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

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

npm publish

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

test npm package

install your package using npm install

npm install

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

result

Happy Coding ! ❤

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Hasitha Charaka 🔵
Hasitha Charaka 🔵

No responses yet

Write a response