How To Create Own NPM Package And Publish It

Introduction

In this article I am going to share with you the simplest way to create your own NPM package and Publish it to npmjs.com. Before that, if you are new to Node keyword or Node.js , you can follow some introductory stuff from below links :
Steps to create and initialize project with VS CODE

Step 1
 

Create new project in Visual Studio code



Here I have created an empty project named helloworldtest , and open it using VS CODE IDE

Step 2 - Create package.json file

Because at first glance we dont have anything , so we should create package.json file by executing following command
  1. npm init
By executing above command , there are some questions you should answer it as follows,



After entering command npm init , you can see that they ask you about name of project, but keep in mind that name should be small latters and can not contain digits

Step-3

Then the next step to specify version



I have given 1.0.0 but you can give version as appropriate for your package release

Step-4

Than provide description about package and entry point of your package when project is loaded



As you can see in the above screenshot, I  have provided a simple description that would publish when someone goes through your package

Step-5

Some more steps like Providing git repository , keywords and Author name to identify the creator of current package



I have entered some details like helloworldtest [which is unique by the way] and test command

NOTE

I have not provided git repository , but if you already have created empty repository you can specify its URL

Step-6

Last step is a summary , in which you can see all the information provided by you



That's it , we have created package.json file with all the needed information

NOTE

Before we publish package we should have account created with npmjs.com site , so that we can publish it by login credential .

Steps to create user and login through VS CODE

Step 1

Firstly we need to add user by using following command
  1. npm adduser
You can see following details after logging in,



By executing the above command they will ask you to enter username and password [which is hidden]

That's it , now user is created with your username.

Step 2

If you want to check whether user is created or not , write following command
  1. npm whoami
After executing above command you will get to know your username:



It shows me that user is created and logged in already

Steps To Publish NPM Package at npmjs.com

Step 1

Create simple js file in VS Code and named it Hello.js or whatever you want and write some code like below,

But before that you should install express as shown below,



Step 2

Write below code into Hello.js file,
  1. // Using NPM Package  
  2. var express = require('express');  
  3. var app = new express();  
  4. // To define Port Number  
  5. var portnumber = 3500;  
  6. // To Print static text  
  7. app.get('/'function(req, res) {  
  8.     res.send('<h1>Hello FOO !!!</h1>');  
  9. });  
Step 3

Now your package is ready to publish by writing the following command
  1. npm publish
Step 4

Now you can see that your package is published to npmjs.com , just go to your account,


That's it , your package is created. And if you want to use my package you can write
  1. npm install hellofootest
Conclusion

So we have learned what is NPM , its usage , and how to create npm package and publish at npmjs.com. If you found something that I have missed , feel free to suggest it to me.

Feedback is always accepted. Thanks


Similar Articles