How to create and publish an NPM package

Learn how to create and publish your own NPM package with this step-by-step guide. Share your code with the community and promote collaboration with ease.

· 4 min read
Cornelius Emase

Cornelius Emase

Introduction

NPM, which stands for Node package manager, is one of the largest software registries on the internet. It houses over a million packages. In this guide, you will learn how to create and publish your own npm package, allowing others to download and use it.

Prerequisites

Before we begin, you need to have the following ready:

  • Node.js: If you haven't already, install it. NPM usually comes pre-installed with Node.
  • Basic knowledge of JavaScript

What is NPM package

NPM is a collection of files, mostly written in JavaScript, that provides specific functionality. This can be a library e.g. JQuery , utility or a tool that will help developers integrate into their code to fulfil certain functionalities.

Benefits of publishing an NPM package

NPM comes along with a lot of benefits, including:

  • Code reusability. This will allow you to share your code with the community of developers and reusing existing packages.
  • Version control. You can easily manage the different versions of the packages you publish.
  • Collaboration. You will be able to collaborate or use other developer’s packages in your package.

Setting up your development environment.

1. Choose a unique package name.

Make sure it hasn't been used already. You can search the NPM registry to verify its availability.

2. Create a new project folder for your package and navigate into it.

Run npm init to create a package.json file. Provide relevant information such as package name, version, description, entry point, and more.

After running the npm init you will have a package.json file which will contain something similar to this:

Image

You will be prompted to answer some few setup questions. Answer them accordingly but if you would like to get default values you can skip by pressing enter or run the following command npm init –yes

We can then modify some few things in our package.json file, the description, the author.

Image

Write your code

The next thing to do is to create the code functionality for our package.

We then need to create the necessary files that will be loaded when other applications would need your modules. You should ensure your entry point matches the one specified in package.json.

In my case we can create a file, index.js and add our code inside.

function HelloNpm(){
   return ‘Hello, this is my first npm package};

module.exports = HelloNpm;

After writing your code, you then need to export it for other users who would download it.

Creating a README file

It is usually the best practice to include a readme file for your package to make it easier for others to understand what your package does.

Image

Publishing Your Package

That is basically all we need to publish our package to npm.

You need to have an account in the npm registry, if not create one where you can publish your package.

Run the npm login and enter your credentials to login.

Image

Now that we’re set up, you can publish your package with this command:

npm publish
Image

By default, NPM publishes to the public registry. If you want to publish a scoped package with public visibility, use:

npm publish --access public

And that's it! Your package is now available for others to use.

Image

Downloading your package manager

Now that your package is available on the registry, others or even you can download and use it by running the following command:

npm install <your-package-name>

npm install kodashcoolnpmtutorial

Image

This will install your package including all the dependencies in it. That’s it. You can go beyond this.

Conclusion

We’ve done all it takes to set up, write our package and publish it. In a simple way, all we have done is initialized our package, written our functionality and finally published it.

share

Cornelius Emase

Software Engineer | Product Manager | Technical writer |