Let’s create and publish an NPM package

hello developers! Welcome back, in this new article I'm going to show you how to create your own npm package and how to publish it. let’s start!

first of all, you should make a directory wherever you want. I will name it as “npmarticle”. and open the command prompt from the directory then type

npm init -y

then open that folder using vscode or any other IDE.

and now you have to create index.js in here we write our package code. in this demo article, I will write a code for calculating BMI for a person. if the user enters the weight and height, this package gonna help to find the BMI of that particular person. (you can create your package for any kind of solution this is only for the demonstration purpose).


Okay, this is our first small package to the npm.

now you can open the package.json file and remove the script property from there if you want. let’s open the command prompt, now what we have to do is create an account on npm, type

npm adduser

and it will ask username, password and email for a new account.

after that, you have to go to your email and verify the account,

if you already have an account in npm just type

npm login

and provide the credentials.

you can identify your username by just typing

npm whoami

now we are ready to publish our first npm package if you just want to know the version you can type

npm version

Okey lets publish the package.


before publishing make sure to use very unique package name

npm publish

congratulations!!! you’ve published your first npm package.

now logged in to npm using the web browser and if you go to your account packages

now you can see your package on the npm!!!

if you click on the package you will direct to the package,

Okay then, the package is shown here but it says that there is no readme file, so let’s create a readme.md file, go to the vscode and make a file called “README.md”.

# this is a package for calculate BMI value
by hasanga lakdinu.

you can enter whatever details of your package here, what its for, how to use stuff like that,

and again save all files, open the command prompt and if you want to change the version now you can change the version like

npm version 1.0.1

(this is not mandatory just for the demonstration).

then publish the package again using

npm publish

now you can see the package updated!

Okay, guys, this is the time to use our npm package.

create another directory using any name mine is npmarticle2, then open the command prompt from here and type

npm init -y

and it will generate a package.json file for us and then we want to install our newly created npm package for this project by typing,

npm i --save npmarticle

and create an index.js file in the new directory in this index file we are gonna use our package code.

const {bmiValue} = require('npmarticle');

now we are going to run our code!!!.. open the command prompt and type

node index

it will give you the answer 15!!!

pretty cool isn’t it? :D :D


in this article I showed you guys how to create our own npm package and how to publish it, I think this will be useful for you, see you later, until then Happy Coding!!!

Software Engineer at hSenid Mobile