Node Package Manager
Presented By: Pooja
Roll No. -77(B)
Introduction to NPM(Node Package Manager)
Overview
Node Package Manager(NPM) is a package manager for the
JavaScript programming language and is the default manager
for the Node.js runtime environment.
It provides a way to manage and distribute packages and
collections of code for use in Node.js projects.
Packages in Node.js
A package in Node.js is a reusable module of code that adds
functionality to our application.
It can be anything from small utility function to a full featured library.
Packages can be installed from the NPM registry.
They are stored in the node_modules folder in our project.
How to Use NPM with NodeJS?
To start using NPM in your project, follow these simple steps :
Step 1: Install NodeJS and NPM
First, you need to install NodeJS. NPM is bundled with the NodeJS installation.
Step 2: Verify the Installation
After installation, verify NodeJS and NPM are installed by running the following
commands in your terminal:
• node –v
• npm -v
These commands will show the installed versions of NodeJS and
NPM.
Common NPM Commands
Initialise npm: initialising npm will create the package.json
file.
npm init -y
The output would be:
{
"name": "<name-of-project>",
"version": "<version-of-project>",
"description": "",
"main": "<default-run-file>", // usually index.js
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Installing all dependencies : If a project has a package.json when we run:
npm install
It will install everything the project needs, in the node_modules folder ,
creating it if its not existing already.
Installing a single package: We can install a specific package by running
npm install <package_name>
Example
npm install express
This will create a dependency object in package.json file :
{
......
......,
"dependencies": {
"express": "^4.18.2"
}
}
Flags that can be used with npm install command
--save : It installs the specified packages and adds them to the dependencies
section of the package.json file.
Command : npm install <package_name> --save
--save-dev : It installs the specified package and adds them to the devdependencies
section of the package.json file.
Command : npm install <package_name> --save-dev
@version : It installs a specified version of a package.
Command : npm install <package_name>@<version>
Example : npm install
[email protected]Update all Packages : we can update all packages to
their latest version y using the following command:
npm update
Update a single package : We can update a particular package to its
latest version by using the command :
npm update <package_name>
Uninstall a package: To uninstall packages using npm , run
the following command:
uninstall <package_name>
For uninstall global packages
Uninstall <package_name> -g
Popular NPM Packages
Express
React
Axios
Nodist
Benefits of NPM
Reusability: Anyone can use any package on npm to build
whatever application they desire.
Support: Since so many developers use npm, it can also be a
community to help you bigger and better projects.
Global and Local Packages: NPM allow us to install packages
both globally and locally.
Dependency Management: NPM makes it easy to manages
dependencies and ensure that projects use the correct version.