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](https://scanskill.com/wp-content/uploads/2022/09/image-97-1024x811.png)
Now, that we have created a git repository let’s clone the repository in our local machine.
![Clone the git repository](https://scanskill.com/wp-content/uploads/2022/09/image-98-1024x393.png)
Clone and open the folder with the code editor of your choice
![Cloning the git repository](https://scanskill.com/wp-content/uploads/2022/09/image-99.png)
after that initialize the project using the npm init
command.
![Initializing as a node project](https://scanskill.com/wp-content/uploads/2022/09/image-100.png)
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](https://scanskill.com/wp-content/uploads/2022/09/image-101.png)
now let’s create another node project to test the code out.
![Creating project for testing npm package](https://scanskill.com/wp-content/uploads/2022/09/image-102.png)
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](https://scanskill.com/wp-content/uploads/2022/09/image-103.png)
//* 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](https://scanskill.com/wp-content/uploads/2022/09/image-104.png)
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](https://scanskill.com/wp-content/uploads/2022/09/image-105-1024x475.png)
Now, check if the name of the package already exists.
![package exists so search for a name without the package](https://scanskill.com/wp-content/uploads/2022/09/image-106-1024x293.png)
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](https://scanskill.com/wp-content/uploads/2022/09/image-107.png)
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
![](https://scanskill.com/wp-content/uploads/2022/09/image-108-1024x336.png)
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](https://scanskill.com/wp-content/uploads/2022/09/image-109.png)
Now, that your package is published, search for your package in our case npm-package-string-transformer
in npmjs.com search area.
![Package search](https://scanskill.com/wp-content/uploads/2022/09/image-110-1024x122.png)
![Package page](https://scanskill.com/wp-content/uploads/2022/09/image-111-1024x603.png)
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.