Create/ Publish your first NPM package

Create a dir/folder to contain your packages code.

mkdir firstnpmpackage

change path to dir/folder you just created.

cd firstnpmpackage

Now run ..

npm init

and fill the required details.

Now create an index.js file in this folder, your folder should look like this

  • index.js
  • package.json

write some code in index.js

#!/usr/bin/env node
function randomNoGenerator(min, max) {
if(typeof(max) !== 'number' && typeof(min) !== 'number') {
min = 0; max = 1;
console.log(Math.random() * (max-min)) + min;

Now let’s modify our package.json a bit to make it work using cli.

"demoproject" : "index.js"

just add this thing in your package.json
demoproject is the command you will use to execute your package through cli.
index.js is the file that will be first triggered.

We are all ready to go ,
but wait wait you will really want to test your package locally before publishing it.

Let’s do it first :

sudo npm install -g ./

Our package is installed in our local machine now,
Let run it type this in your cli and see the result.


So, now we are done with testing ..
Let’s publish it now ,

To make it published you first need to signup on

once you are done signing up!
Run :

npm adduser

Give your login credentials..

Now we are ready to go ..
open terminal in your root directory..

and Run :

npm publish


NOTE : if it gives any error goto package.json and change name of your project any package with the same name might be published before…

my GitHub: Deep1144
my npm account: deep1144

Follow for more :

Angular | React | Node | Redux | Flutter and all about web development