NodeJS NPM: A Beginner’s Guide to Node Package Manager

NPM stands for Node Package Manager, used to install different Node.js packages. NPM is the core of Node.js, it is an online repository of thousands of Node.js packages use for different functions. These packages are free to install and can be used in the creation of different types of applications like express is the package used to write Node.js in an easy way, Mongoose is a package used to connect with the MongoDB database, and so on.

Anybody on the internet can create their own packages and upload them into the NPM repository and then everyone can use it by installing them inside their projects. There are different platforms for creating servers but one of the many things that make Node.js stand out of them is the power of NPM. NPM has packages for literally anything.

Initiate NPM

We can initiate NPM inside a project to install any packages available on the NPM repository.    

Syntax

npm init

This command asks you many questions regarding the project in which you are initiating NPM, you can enter the value or leave it blank and press enter until it gets completed.

Output:

Initial NPM

If you don’t want the NPM to ask you those annoying questions every time you initiate it, then you can use a “-y” flag.

npm init -y

Output:

Initial NPM With Y Flag

Installing Packages in NodeJS using NPM

Packages can be installed inside a project by using a single-line command.

Syntax:

npm install packageName

packageName is the name of the package you want to install inside your project.

This syntax can be shorter by replacing the keyword install with i.

npm i packageName

Example:

npm i express

Output:

Install Package

Update Packages using NPM

The packages contain some bugs and in order to make them better, the organization provides different updates, we can update it by using the single line command rather than uninstall it and install it again.

Syntax:

npm update packageName

packageName is the name of the package you want to update.

Example:

npm update express

Output:

Update Package

Uninstall Packages using NPM

The packages take up a large amount of space, so if you mistakenly install any packages since some packages have a very similar name then we can also uninstall them.

Syntax:

npm uninstall packageName

packageName is the name of the package you want to remove from the project.

Example:

npm uninstall express

Output:

Uninstall Package

NPM Global vs Local Installation

By default the NPM install packages inside a project folder node_modules, this is called local installation. But it is very obvious that if you are working locally and creating projects for personal use then it is kind of annoying to install it again and again, so we can install it globally by using a “-g” flag

Syntax:

npm install packageName -g

packageName is the name of the package you want to install globally so that you don’t have to install it manually inside different projects.

Example: 

There is a package named nodemon used to automatically restart the server when a change happens. If we install it by this command

npm i nodemon

then it is required to install it again and again in different projects to use it. But if it is installed using the “-g” flag then there is no need to install it inside an individual project.

npm i nodemon -g

Output:

Initial Nodemon With G Flag 1

Summary

NPM stands for Node Package Manager and provides the power to install any packages from the online NPM repository which helps in making different types of projects. We can install a package, uninstall unused packages and update the packages using NPM. NPM contains every type of package you need for your projects. If you are tired of installing a package again and again when creating a new project then you can simply install that package globally using the “-g” flag, then you can use it inside any project directly. Hope this article helps you to learn about the NPM.

Reference

https://www.npmjs.com/

Aditya Gupta
Aditya Gupta
Articles: 113