NodeJS NPM

NodeJS NPM: In Node JS, the NPM is the core aspect for any application to develop. It specifies a large number of libraries that act as the best tool for node.js developers and also speeds the development of the application process. To learn about Node JS you need to learn about NPM. So let’s see some basics of NPM and eventually start with Node JS also.

What is NPM?

Many of you might consider what is NPM and Why is that much important to learn NodeJS. So let’s learn about the NPM in detail. The NPM stands for Node Package Manager which is a default package for NodeJS and written in JavaScript. It was created by Isaac Z. Schlueter and released in 2010. It is the world’s largest software registry that completely open -source. so that the developers can easily share in the world and it manages all the NodeJS modules and packages.

The most important functionalities of NPM are:



  • In the official site, it provides online storage capacity for packages and modules so the developers can easily search for these packages and modules.
  • The CLI(Command Line Interface) is used by the developers to interact locally with their systems.

NOTE: Any package or module of Node JS must be installed through NPM only and there are lot more functionalities of NPM in Node JS.

Why NPM is needed?

To use NodeJS, NPM is the most important aspect and it is used for multiple purposes but a few are listed below.

  • With NPM, you can include pre-built packages into the project.
  • The NPM allows you to run the packages without downloading it.
  • The NPM helps you to manage and maintain multiple versions of codes with their dependencies.
  • With the help of an update in the code, the NPM automatically updates your application.
  • The NPM allows you to download multiple standalone tools that can be used easily.
  • Most of the developers use NPM to share their code between other NPM users worldwide.
  • The NPM works like a community that provides information regarding other developers who work on the same projects.
  • It also restricts the code for particular developers and virtual teams using organizations.
  • Finally, the NPM creates multiple organizations for coordinating package maintenance, coding, and developers.

Thus, the various reasons for the necessity of NPM in Node JS. Let’s learn some fundamentals about NPM packages.

NodeJS NPM Packages

A package is the group of source code that tied up into separate logic codes. For developers, these packages are the best choice because they reduce the size of the code and handle the complexity. there are millions of packages that work differently.  The NPM is the library that holds all the packages with the support of Node JS application development.

Some of you might don’t know about what packages actually serve. So we have listed some of them below.

  • express.
  • debug.
  • react.
  • nodemon.
  • moment.
  • request.
  • body-parser.
  • lodash.
  • async.
  • babel-core.

NOTE: You need to install NPM in tour PC before you install any of these packages.

NodeJS NPM Installation

From the version of NodeJS 0.6.3, the NPM  includes NPM in the default package in Node JS. So there is no need for NPM to install separately in the PC. If you want to learn how to install NodeJS in your PC,  then check the article NodeJS installation on Windows and MAC OS.

To check whether NPM is installed or not, then you the following command.

npm -v

After checking for NPM, you need to learn about how to install, delete or update packages with NPM CLI. First, let’s learn about the types of packages.

Local and Global packages

Based on the mode of installation, the packages are classified into two categories.

  • Local Packages
  • Global Packages

Local Packages

The Local Packages are installed inside the directory where you can execute the install commands and accessed them by your projects. Inside your main project directory, the local packages are under the node_modules folder. Use the following command to install local packages.

npm install <package-name>

Global Packages

These are the packages that are installed in one place and you execute your run commands elsewhere. These are the Global packages that can be used in any projects that are inside your PC. Use the following command to install the global package.

npm install -g <package-name>

Some global packages used most often

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

The major difference between local and global packages is that global packages can be used from anywhere in your system. The usage of the local package is limited but in general-purpose it is the best method to practice the packages. You may have multiple node js projects in your PC that having the various versions of packages.

If you update the global package then it will update it all projects that you have used. This is the major drawback to use packages globally which increases complexities. The resource utilization is not good practice but it provides less negative impacts. when there is CLI executable command you can install the package globally.

To check how many global packages are installed then use the following command.

npm list -g --depth 0

To remove any package from your system then use the below command.

npm uninstall <package_name>

Make sure you included the save keyword to save all your commands. For that just type as the following command.

npm install <package_name> --save

Now your module has been added to the package.json file.

Package.json File

Package.json is the main important aspect of your application. It’s just a manifest file to maintain the metadata of your particular project. The package.json file will be the root folder of Node JS application and it looks like as mentioned below.

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

The above code is the package.json file that contains various properties used in the project. Let’s see them in detail.

  • Name: It contains the name of the project given by the user.
  • Version: It provides the application version and should follow some semantic versioning rules.
  • Description: It specifies the purpose of the application and technologies used like React, MongoDB, etc.
  • Main: Specifies every point/file of the application.
  • Scripts: Provides a list of scripts that are required to the application execute properly.
  • Author: To defines the name of the main developer of the entire project.
  • License: The application confirms are mentioned in the key-value pair.
  • Dependencies: Provides the third party packages or modules installed through NPM.
  • DevDependencies: These dependencies are used in the only development part of the application.
  • Repository: It holds the information about the type and URL of the repository where the application code will be present.
  • Bugs: It contains URL and Email where the application should be reported.

That’s it about the NodeJS NPM. Now you can easily create the NodeJS application using NPM with the help of this tutorial.