LEARNNode CLI Automation

What Is A Node Module?

If you are working with JavaScript, there is a strong chance that you have heard about or used node modules. Let’s discuss it now so you can have a clear understanding.

Node Modules or npm Package

The node modules or the npm packages are the autonomous piece of code that you can use in your project. They provide a certain feature or an ability to do something. You can use Node Package Manager or npm to install them in your project.

All of these modules have their own context. So they don’t interfere with other modules or pollute the global scope.

npm Package Example

Let’s take a look at a node module called axios that allows you to make asynchronous API requests in your Node.js application. You can install it by running the following command in your terminal:

npm install axios

Now you need to import it in your project by requiring it like this:

const axios = require(‘axios’);

Now you can make API requests with axios like this:

const getData = async () => {
try {
const res = axios.get(`API`);
console.log(res);
} catch (error) {
console.log(error);
}
};

You have seen how I used a third party node module and used it to perform a certain task in my application. I don’t have to write a long code of making API requests. I just went ahead and used a node module for it.

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)