Create and Deploy NPM Packages

How to Create and Deploy NPM Packages

Let’s Discuss NPM Packages…

Node.js is a popular server-side programming language used by millions of developers all over the world.

We use a variety of packages to make development easier when working with Node.js.

The Node Package Manager is in charge of all of these packages.

Hire Dedicated Developers

Introduction

A package manager is a software that keeps track of your project’s versions, packages, and dependencies.

A package manager assists in the management of dependencies as a project develops in size.

You can use it for all versions and in collaboration with other people.

It saves money by minimizing the number of dependencies between developer teams.

We use the NPM registry, which is a centralized repository of Node.js packages, to install them.

We’ll learn how to make a Node.js package and put it on the NPM registry for public use in this tutorial.

NPM registry linkhttps://www.npmjs.com/

Create an NPM Account

Let’s get started by making an NPM account.

To sign up for npmjs, go to their website and click the signup button.

Npmjs link – https://www.npmjs.com/

Enter a username, email address, and password.

You must now confirm your account.

Check the inbox of the email address you supplied for the verification email and click on it.

Your npm account will be verified as a result of this.

You’ve created an NPM account and are ready to start publishing packages.

To begin publishing packages, login into your npm account on your terminal.

To begin, create a new folder and launch your terminal.

Enter the following command:

npm login

Enter your username, password, and email address now.

We have successfully signed in.

Let’s Code

A package.json file should be included in every package published to the NPM registry. Let’s begin by setting up a package.json file.

The package’s minimum requirements. A name and a version can be found in a JSON file. Let’s put those details into the file.

{
   “name”: “clist-node”,
   “version”: “1.0.0”
}

In the package, give it a new name. Because each NPM package should have its name, we use JSON.

For making API calls, we’ll use the Axios package in our sample NPM package. Use the following command to install it.

npm i axios

Note: Visit the Axios NPM package page for further information.

Axios link – https://www.npmjs.com/package/axios

  • We’ll use the clist API, which returns a list of contests such as hackathons and programming competitions.
  • Get an API key by creating an account on the clist website.
  • The index.js file would be the package’s entry point. In the same directory, create a file called index.js.
module.exports = async function getContests() {
   const axios = require("axios"); // Importing the Axios module to make API requests
   var result;

   const username; // Insert Your Username here
   const api_key; //Insert API key here

   await axios // Making a GET request using axios and requesting information from the 

API.get("https://clist.by/api/v1/json/contest/?username=" + username + "&api_key=" + api_key + "&limit=20&end__gt=2020-09-19T00%3A00%3A00")
   .then((response) => { // If the GET request is successful, this block is executed
      return response; // The response of the API call is passed on to
         the next block
       })
     .then((contests) => { 
    // In this block, we store the response data into a variable 'result'
   result = contests.data.objects;
  })
   .catch((err) => {
      console.log(err); // Error handler
   });
   return result; // The contest data is returned
};

To run this package, you’ll need an API key. As a result, if you install it, it might not start right away.

It’s time to deploy the package now that we’ve programmed it.

Deployment NPM

To deploy the package, enter the below command:

npm publish

As soon as your package is published, you’ll receive an email from NPM letting you know!

Use the Packages

Let’s get our package installed and tested.

Create a new folder and start a package.json with the following command:

npm init -y

It’s worth noting that the -y option builds a package. Without an interactive procedure, JSON

Let’s now install the npm package we just released.

npm i clist-node

Create an index.js file and let’s code

const clist = require("clist-node"); // import the installed package

clist()
.then((res) => {
// If the function successfully retrieves the data, it enters this block
console.log(res); // Print the contest data on the console
})
.catch((err) => {
console.log(err); // Error handler
});

Run this file by using the command:

node index.js

Our NPM package would display contest data on the console. In addition to using the collected information on our front end, we can save it in a database or process it further using it. The options are limitless.

NPM Package Run Command

It was a success to install and test our NPM package!

In Conclusion of NPM Packages

To recap what we performed, we did the following:

  • From npmjs.org, I created an NPM account.
  • Using the command npm login, log in to the terminal using your npm credentials.
  • The npm init -y command was used to create the package.json file.
  • I wrote some code.
  • npm, publishing was used to deploy the package.
  • Using npm I <package-name>, use your deployed package.

Reference NPM Packages

Similarly, https://docs.npmjs.com/

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply