Mastering Node.js (19 Blogs) Become a Certified Professional

Node.js NPM Tutorial – How to Get Started with NPM?

Last updated on Sep 26,2020 4.5K Views

Swatee Chand
Sr Research Analyst at Edureka. A techno freak who likes to explore... Sr Research Analyst at Edureka. A techno freak who likes to explore different technologies. Likes to follow the technology trends in market and write...
1 / 7 Blog from Node.Js Fundamentals

NPM is the core of any application that is developed in Node.js. It provides huge sets of libraries which acts as great tools for the Node.js developers and speeds up the complete application development process. Thus, in order to get started with Node.js, first, you need to have a clear understanding of NPM. This article on Node.js NPM Tutorial will help you in understanding the basic needs and fundamentals of NPM and eventually give a head start in Node.js.

Below are the topics I will be covering in this article:

Let’s get started.

npm logo - Node NPM Tutorial - EdurekaWhat is NPM?

NPM stands for Node Package Manager. It is the default package manager of Node.js that is completely written in JavaScript. It was developed by  Isaac Z. Schlueter and released in the market in 2010. Since then, it was responsible for managing all the Node.js packages and modules. It is the world’s largest software registry which is completely free and open-sourced and developers use it for sharing software across the globe.

The main two functionalities of NPM are:

  1. It provides online repositories for packages/modules for Node.js which you can easily search online on their official site.
  2. It also provides a Command Line Interface (CLI) which helps the developers in locally interacting with their systems.

One thing which you must remember is, any package or module required in a Node.js project is needed to be installed via NPM. Apart from this, there are a lot more functionalities for which npm is being used. In the next section of this Node.js NPM Tutorial, I will be talking all about them.

Need for NPM

As I have already mentioned, NPM is used for various purposes. Below I have listed down the most fundamental ones:

  • It helps in incorporating the pre-built packages into our project
  • It assists in downloading various standalone tools which can be used right away
  • Using npx, you can even run packages without having to download it
  • Developers often use NPM to share their code with other NPM users across the globe
  • It creates various organizations for coordinating package maintenance, developers and coding.
  • Also helps in restricting the code to specific developers and forming virtual teams using orgs
  • Helps in managing and maintaining various versions of codes and their dependencies
  • NPM automatically updates the application with the update in the underlying codes.
  • It works like a community where you can find other developers working on similar projects or tasks.

Now, that you know why NPM is necessary for a Node.js project, let’s delve deeper into it NPM fundamentals. To begin with, let’s first understand what actually are the npm packages or modules.

NPM Packages

A package is a group of code bundled up into a separate logical unit. These packages are handy tools for the developers as they help in abstracting the complexity and reduces the code size. There are around 800,000+ readymade code packages available, each one of which serves a different functionality. NPM is a kind of library which holds all these packages within to support Node.js application development.

But, if you don’t know exactly which package will serve your purpose best, it will be the same as finding a needle in the hay. Thus, to give you a little push, below I have listed down the most popularly used libraries of NPM:

  1. express
  2. body-parser
  3. nodemon
  4. lodash
  5. babel-core
  6. async
  7. debug
  8. react
  9. request
  10. moment

But before you can use any of these packages you need to install NPM in your system.

In the next section of this Node.js NPM tutorial, I will be sharing how to install NPM and various packages.

NPM Installation

Node.js version 0.6.3 onwards, NPM was included as a default package in Node.js. Thus, there is no need to install it explicitly. To know how to install Node.js in your system, you can refer to my Node.js Installation article.

In order to check whether NPM is already there in your system or not, you just need to type in the below command.

npm -v

Now that installing NPM is out of the way, let me show you how to install, update and delete packages using NPM CLI (Command Line Interface). But before you start installing or downloading your packages, one thing you need to know is the packages can be installed in two ways, about which I will be talking in the next section of this Node.js NPM Tutorial.

Local and Global Packages

As I have already mentioned, the packages are categorized under two categories depending on their mode of installation:

  1. Local Packages
  2. Global Packages

1. Local Packages

These are the packages which are installed within the directory where you will be executing the install command and will be accessible by only your project. Local packages are contained by a node_modules folder under your main project directory.

Below is the command to install packages locally:

npm install <package-name>

2. Global Packages

These are the packages which are installed at a single place in your system irrespective of the place where you execute your run command. They are called global packages as they can be used by any of the projects present in your system. To install a package globally, you need the below-given command:

npm install -g <package-name>

Few of the most used global packages are:

  • npm
  • create-react-app
  • vue-cli
  • grunt-cli
  • mocha
  • react-native-cli
  • gatsby-cli
  • forever
  • nodemon

The major difference between local and global packages is that the Global packages are used for anything that is needed to be accessed from the shell. On the other hand, the usage of local packages is typically limited to your applications or projects. But in general, it is a good practice to install the packages locally. This is because you might be having a number of Node.js projects in your system having a different version of each of the packages used in them.

Now, if you update a global package then it will update it in all the projects it has been used in your system. It can cause a huge disaster as few of these packages might become incompatible with the further dependencies used in the projects. But in case of local packages, you can always keep your version of a package and update it according to your own needs. In terms of resource utilization, it might seem like a waste of memory but it has relatively less negative impacts.

Thus, you should make a package global only when it has a CLI executable command and can be reused throughout the projects in your system.

You can also check how many global packages you have in your system by typing in the below command.

npm list -g --depth 0

In case you want to remove a package from your system you can just type in the below command:

npm uninstall <package_name>

Here I would like to slip in a piece of advice, that is whenever you are installing the packages make sure you include –save flag with it.

npm install <package_name> --save 

It will ensure that your requested module has been added your package.json file. 

Now, what is package.json and why it is needed, I will tell you in the next section of this Node.js NPM Tutorial.

Package.json File

The package.json file in Node.js is considered to be the heart of an application. It is nothing but the manifest file that holds the metadata of the project. Thus, it is very important to understand this file in order to work a Node project. The package.json file is usually present in the root folder of any Node.js application and looks like the below.

{
"name": "samplenode",
"version": "1.0.0",
"description": "Edureka demo on how to build a Node.js application",
"main": "script.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Edureka",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.16.4",
"express-handlebars": "^3.0.2",
"nodemon": "^1.19.0"
},
"devDependencies": {},
"repository": {},
"bugs": {}
}

As you can see in the above code, package.json file holds definitions of various properties used in a project. Let us understand each of these properties in detail and see why they are important in a Node.js project:

  1. Name: Holds the name of the project provided by the user
  2. Version: Depicts the version of the application and must follow the semantic versioning rules.
  3. Description: Holds the explanation & purpose of the application, technologies used like React, MongoDB, etc.
  4. Main: Points to the entry point/file of the application
  5. Scripts: Contains the list of scripts which are required to be included in the application to execute properly
  6. Author: Holds the name of the primary developer of the project
  7. License: License to which the application confirms are mentioned in this key-value pair
  8. Dependencies: Depicts the list of 3rd Party packages or modules installed using NPM
  9. DevDependencies: Dependencies that are used only in the development part of the app are specified here
  10. Repository: Contains the information regarding the type & URL of the repository where the code of the application resides
  11. Bugs: URL and email where the bugs in the application should be reported are mentioned here

With this, we come to an end of this Node.js NPM Tutorial. Hope you were able to understand what exactly is NPM and how it helps in building a Node.js application. Now you can get your hands dirty with Node.js. To get started, you can refer my Node.js Tutorial article. 

If you found this “Node.js NPM Tutorial” relevant, check out the Node.js Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this Node.js NPM Tutorial and we will get back to you.

Upcoming Batches For Node.js Certification Training Course
Course NameDateDetails
Node.js Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Node.js NPM Tutorial – How to Get Started with NPM?

edureka.co