Certainly! To format the provided text for a GitHub README file, you can use Markdown, which is a lightweight markup language. Here's the text you provided formatted with Markdown:
markdown
Create and Deploy NPM Packages
September 28, 2020
Topics: Node.js
Node.js is a popular server-side language that is used by millions of developers worldwide. While building Node.js projects, we use various packages that make development easier. All of these packages are managed by the Node Package Manager.
Table of Contents
Introduction
A package manager is software that manages versions, packages, and dependencies for your project. When a project grows in scale, a package manager helps to manage dependencies. It works perfectly across versions and even in collaborative projects. It cuts costs by reducing dependency problems across developer teams.
To install Node.js packages, we use the NPM registry, which is a centralized repository of packages. In this article, we will learn how to create a Node.js package and deploy it on the NPM registry for public use.
For more background on Node.js and various related technologies, check out some amazing Section.io articles below:
Create an NPM Account
To get started, let’s create an NPM account:
- Go to the npmjs website and click on the sign-up button.
- Enter a username, email address, and password.
- Now, you have to verify your account. Check the inbox of the email address that you provided and click on the verification email. This will verify your npm account.
You have successfully created an NPM account and can start publishing packages.
You need to sign in to your npm account on your console to start publishing packages.
To do this, open a new folder and start your terminal. Enter the command:
npm login
Now, enter your username, password, and email ID.
Let’s Code
Every package published to the NPM registry should have a package.json file. Let’s start by initializing a package.json file.
The minimum requirements of a package.json file are a name and a version. Let’s enter those pieces of information into the file.
{
"name": "clist-node",
"version": "1.0.0"
}
Enter a different name in package.json because every NPM package should have a unique name.
In our sample NPM package, we are going to use the axios package for making API calls. Install it by using this command:
npm i axios
We’ll be using the clist API that returns a list of contests like hackathons, programming contests, etc.
Create an account on the clist website and get an API key.
The entry point of our package would be the index.js file. Create a file named index.js in the same directory.
javascript
module.exports = async function getContests() {
const axios = require("axios");
var result;
const username;
const api_key;
await axios
.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) => {
return response;
})
.then((contests) => {
result = contests.data.objects;
})
.catch((err) => {
console.log(err);
});
return result;
};
Note: This package requires an API key to run. Therefore, it might not run directly if you install it.
Now that we’ve programmed the package, it’s time to deploy it.
Deployment
To deploy the package, enter the command:
npm publish
In a moment, you’ll get an email from the NPM registry indicating that your package has been successfully published!
Using the Package
Let’s install and test our package.
Open a new folder and initialize a package.json by using the command:
npm init -y
Note: The -y option creates a package.json without an interactive process.
Now, let’s install our published npm package.
npm i clist-node
Create an index.js file and let’s code.
javascript
const clist = require("clist-node");
clist()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
Run this file by using the command:
node index.js
The contest data from our NPM package would be printed out on the console. The retrieved data can be used to dynamically render elements on our front-end, store them in a database, or process the data further. The possibilities are endless.
Result
We have successfully deployed and tested our NPM package!
Conclusion
To summarize what we did:
Created an NPM Account from npmjs.org.
Login to the terminal using your npm credentials using the command, npm login.
Initialized the package.json file using the npm init -y command.
Wrote code.
Deployed the package using npm publish.
Use your deployed package using npm i.
Further Readings
For more knowledge on NPM packages and JavaScript, check out the articles below.
NPM Documentation
Async/Await vs Promises in JS
HTTP Requests - Mozilla Developer Network