How to Create and Submit a Node Package to NPM Registry

3 min read

Have you ever thought of how developers create NPM packages that you use in your Node projects? This is what I will be showing you today. By the end of this tutorial, you will have your first Node package created and published to the NPM Registry.

Creating a Node Package

Firstly, create a new folder somewhere on your computer and give it any name you like. For example, I have created a folder, named appdev-pkg, in the Document directory of my computer. 

Once you have created the folder, navigate into the folder, using the change directory command. 

Create a package.json file inside the folder by typing the command below.

npm init --yes
image of package.json file in node application using npm init --yes

Open the folder in your favorite code editor. This could be Visual code editor, Sublime or Atom.

Add a new file and name it index.js. The index.js file is the entry point to the package we are creating.

Now, let us export a simple function that multiplies the value of two different variable, just for demonstration purpose. Copy and paste the code snippet below into the index.js file.

module.exports.multiply = function(a, b) {
     return a*b;

Publishing a Node Package to NPM

The next step is to register on NPM registry in order to publish the package, or simply login if you already have an account. This can be done through the terminal by following the steps below.

Simply type npm login if you already have an account, otherwise, type npm adduser in order to create a new account. A prompt will show up and you will need to fill out your username, password and email address. Once you sign up successfully, an email will be sent to you, to verify your email account before you can be allowed to publish on NPM.

image of npm adduser to signup on NPM registry

In order to publish the Node package, simply type:

npm publish --access public
image showing how to publish node package on NPM

It is important to give a unique name to your package, in order not to get an error response when trying to publish to NPM registry. This means that you should not use the same package name as mine.

Once the package has been published, it’s time to now make use of it our next Node application.

Using Node Packages in Project

Firstly, create a new project folder named test-node-pkg or any similar name you prefer. Navigate into the created folder and open in your favorite code editor such as VS Code, Atom or Sublime.

Run the command below, within the folder, in order to generate the package.json file for our new project.

npm init --yes

We need to install the package we published earlier, using the command below. 

npm i appdev-pkg

The ‘i’ is the short form  for install, while the appdev-pkg is the name of the package I published to NPM registry earlier in the tutorial. Make sure to change the package name to the name you used when submitting to NPM.

Notice that the node modules folder has been installed, within the project and the NPM package is now available for use.

The next step is to make use of appdev-pkg as a node module within our new project (test-node-pkg).

Firstly, create a new file named index.js in the root of the project.

Secondly, load the Node module and store it in a variable for easy re-use as shown below.

var appdev = require('appdev-pkg');

Now, let’s test the package by making a call to the multiply function in the appdev-pkg, and also pass two parameters that will be multiplied. 


The final step here is to confirm that the result of the operation above is 6. That is, 2 multiplied by 3. In order to achieve this, let’s store the result of the operation in a variable and log it to the console. The final code in the index.js file should be similar to the code snippet below.

Now, we need to pass the index.js file to Node for processing in order to get our result. Simply go back to the terminal and run the command below, from within the project folder.

node index.js
result of the multiplication operation using the published node package

Publishing an Update of a Package

Let’s update our published package with another function that can add two numbers. Simply add the code snippet below into the index.js file of the Node package. In my own case, my Node package is appdev.pkg

module.exports.add = function(a,b){
    return a + b;

In order to publish an updated version of the package to the NPM registry, we need to navigate into the package folder(appdev-pkg) in the teminal and type the command below.

npm version minor
image showing how to update node package published on NPM

This command automatically updates the version number, as shown in the image above. A bug fix can be updated using npm version patch, while a major change in the package can be updated using npm version major. We simply using the minor because we only added a new function, that doesn’t break our first published function. 

Finally, publish the update by using the npm publish command.

image showing an updated version of node package published on NPM

HTTP Put and Delete Requests – Building Restful APIs…

In order to update or delete an information, we need have the object ID. Let's analyze the code block below for handling HTTP Put...
1 min read

Validating POST Request – Building Restful APIs using Express:…

It is important to have a validation mechanism that validates inputs when building web services. Read more as we use the joi package to...
2 min read

Building Restful APIs using Express: Part 6 – POST…

In this section, we will discuss how to use http POST request to create a new radio station. We will test endpoint using the...
2 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a tutorial from us, get weekly updates in your inbox.