ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

How To Create And Publish An NPM Package

How To Create And Publish An NPM Package
How To Create And Publish An NPM Package

Let’s learn how to create and publish an npm package on npmjs.com.

Introduction

npm is a JavaScript package manager developed by npm, Inc. The Node.js JavaScript runtime environment’s default package manager is npm. It consists of a command line client, often known as npm, and the npm registry, an online database of public and paid-for private packages.

So, let us see how to create and publish an npm package.

Prerequisites

Creating a package

Let’s look at creating a package that takes in a string and gives out two values, they are:

  • Lowercase
  • Uppercase

The function within our package will take in a string and will return a Lowercase and an Uppercase value of the same string. But before starting to create an npm package let’s create a git repository.

Initializing a git repository

Creating a git repository for the npm package
Creating a git repository for the npm package

Now, that we have created a git repository let’s clone the repository in our local machine.

Clone the git repository
Clone the git repository

Clone and open the folder with the code editor of your choice

Cloning the git repository
Cloning the git repository

after that initialize the project using the npm init command.

Initializing as  a node project
Initializing as a node project

As we have given the entry point to the application as index.js, create the same file here.

After that is done let’s get started with writing the code that converts the given string into lower and uppercase.

// index.js

function stringTransformer(string) {
  return {
    lowercase: string.toLowerCase(),
    uppercase: string.toUpperCase(),
  };
}

module.exports = stringTransformer;

So now, that the code is written the package how do we test this, to solve this issue we are going to use the command npm link in the current directory which contains the npm package we created.

Creating an internal link for package testing
Creating an internal link for package testing

now let’s create another node project to test the code out.

Creating project for testing npm package
Creating a project for testing the npm package

Now, that the test-npm-package project is created, open it with a code editor. Now the problem is how do we use our created npm package, which is present in our local machine. For that, we will be using npm link, again but this time it will be used to install the package we created locally.

using npm link to install locally created package
using the npm link to install locally created package
//* index.js of test-npm-package project

const stringTransformer = require("npm-package");

console.log(stringTransformer('HeLlO WoRlD!'));

Finally, let’s run the test-npm-package application and look at the results.

results received while using the package
results received while using the package

So we have successfully created an npm package and, tested it on our local machine. But our goal is to create and publish an npm package, we are still missing the publishing part. So, let’s look at publishing next, to achieve the goal to create and publish an npm package.

Publishing the package

To publish the created package, you need an account on the npmjs site, then log in.

login into your account
login into your account

Now, check if the name of the package already exists.

package exists so search for a name without the package
The package exists so search for a name without the package

we are going to name the package as, npm-package-string-transformer as the name is not taken. For that, we need to update the name of the package.json file to the above name.

Changing the name of the npm package
Changing the name of the npm package

Now, that the package name is decided let’s publish our package. For publishing the package follow the steps below.

npm login

Login to your npm account through the terminal by writing npm login on your terminal, as shown in the image below.

$ npm login
Login into npmjs through terminal

Now that you have logged in, let’s checkout a package is published.

npm publish

To publish your package, in the terminal write npm publish, it will publish your package to the npm repository if the name is available. An example of this is given below in the form of images.

$ npm publish
Package published
Package published

Now, that your package is published, search for your package in our case npm-package-string-transformer in npmjs.com search area.

Package search
Package search
Package page
Package page

So, we can see that the npm package is published to the npm registry. Likewise, we have completed our goal to publish an npm package in this section.

Conclusion

So, in this article, we learned, how to create and publish an npm package. We also learned how to test the packages locally.

Sign up for daily dose of tech articles at your inbox.
Loading