NodeJs

NodeJs Tutorial #03: Add npm packages into NodeJs projects.

how to add npm package to project

In the previous knowledge bit, we have given the introduction about command line and we have introduced some most common command line. Now in this knowledge bit, we will learn about npm, we will see that why we use npm. We will also learn to include npm packages in our program.

Introduction to npm

What is npm?

Npm is a package manager for JavaScript and Nodejs, and world largest ecosystem of open source libraries. Npm is a collection of libraries which we can include in our code.

What is mean by Package Manager?

Let suppose we have a problem to solve, so we write a code to solve that specific problem. If we encounter the same problem later in a different project then we can reuse that same piece of code. So the individual code or files that contain reusable code are called modules. And the package is nothing but a directory with one or more module along with a special file called package.json. So the word package and module can be used interchangeably.

NodeJs tutorial: npm packages

 

Now let see how npm helps in managing these packages. Suppose a developer A write JavaScript code to solve some particular problem. Now npm allow developer A to publish that code into the npm registry so that other developer, who are facing the same problem can just reuse developer A code. And whenever developer A make changes in code, then npm makes it easy for the developer who is dependent and using that code to check for those updates and download them.

So npm is a way to share your code with other developers and reuse the code from other developers and easily manage different version of the code.

Add npm packages to the project:

Getting npm:

Npm comes with nodeJS, so if you want to have npm, go and download NodeJs. In the previous lesson, we have discussed that how we can download and install nodejs. So if we want to check the version of npm then open terminal and type npm –v.

Add npm packages into project - Downloading npm packages

Currently, I have installed 5.6.0 npm version, make sure you have the latest version of npm.

How to install any Package:

To install npm package, we first need to do is open command line and write npm install and then the name of the package that we want to install.

 

NodeJs tutorial: Most frequently installed npm packages

 

Now, let see how we can install and use any npm package into our project. The first thing we need to do is create the new folder. And open the terminal in that folder directory. In our case, our folder name is “npmAPP”. Now create new JavaScript file in that directory, we are using the sublime text editor to create the new JavaScript file and named that file App.js.

Now go ahead and include package into your project, open terminal and install the package that you want to use. In our case, we are using cat-me package, this package is consist of JavaScript code which returns random ASCII cat.

To include cat-me package write the following code in your terminal:

npm install cat-me       

 

NodeJs tutorial: install Cat-me package in windows

 

You can notice that cat-me package is successfully added to our project.

So after adding the package to the project, let’s open your app.js JavaScript file that we created earlier in this tutorial. In this JavaScript file add required command, required command is used to call the package to JavaScript file. this is how we can call the cat-me function to JavaScript file.

 

var catMe = require('cat-me')       

 

This command calls the cat-me library and to print cat in the console. We will use console.log.

 

console.log(catMe());    

 

Save that JavaScript file and then open up your terminal and run the app.js file using the following command:

 

node app.js       

 

NodeJs tutorial: running app.js from Cat-me package

 

You can notice that we have got the random cat. If we run JavaScript file one more then we get the different cat:

 

So this is the code that someone else wrote, we have just included the package. Similarly, there are thousands of package available which you can use in your project.

Conclusion:

In this NodeJs tutorial, we have given the introduction and then we have discussed package manager. We have also seen that how we can install npm packages and add and use any of those npm packages in our project.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

To Top