0% found this document useful (0 votes)
26 views28 pages

Ch-4 Node Package Manager

Node Package Manager (NPM) is used to install and manage dependencies in Node.js projects. It installs packages locally or globally and manages them through the package.json file. The package.json file contains metadata about the project like name, version, dependencies, and repository information. It allows NPM to install dependencies and helps other developers understand the project.

Uploaded by

Sujeet Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views28 pages

Ch-4 Node Package Manager

Node Package Manager (NPM) is used to install and manage dependencies in Node.js projects. It installs packages locally or globally and manages them through the package.json file. The package.json file contains metadata about the project like name, version, dependencies, and repository information. It allows NPM to install dependencies and helps other developers understand the project.

Uploaded by

Sujeet Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter-4

Node Package Manager


Contents
4.1 What is NPM?
4.2 Installing package locally
4.3 Adding dependencies in package.json
4.4 Installing packages globally
4.5 Updating packages
4.6 Managing Dependencies
What is NPM?

• NPM (Node Package Manager)


• Default package manager for Node.js and is written entirely
in JavaScript.
• Developed by Isaac Z. Schlueter, it was initially released in January 12,
2010.
• NPM manages all the packages and modules for Node.js and consists of
command line client npm.
• It gets installed into the system with installation of Node.js.
• The required packages and modules in Node project are installed using
NPM.
• A package contains all the files needed for a module and modules are
the JavaScript libraries that can be included in Node project according
to the requirement of the project.
• NPM can install all the dependencies of a project through
the package. son file.
• It can also update and uninstall packages.
• In the package. son file, each dependency can specify a range of valid
versions using the semantic versioning scheme, allowing developers to
auto-update their packages while at the same time avoiding unwanted
breaking changes.
• npm is open source
• The top npm packages in the decreasing order are: lodash, async, react,
request, express.
• NPM consists of two main parts:
• a CLI (command-line interface) tool for publishing and downloading
packages, and
• an online repository that hosts JavaScript packages
• Checking and updating npm version:
• NPM comes bundled with Node.js installables after v0.6.3 version.
• To verify the same, open console and type the following command
and see the result −
• Version of npm installed on system can be checked using following
syntax.
Syntax: npm -v
What is a Package?

• A package in Node.js contains all the files you need for


a module.
• Modules are JavaScript libraries you can include in your
project.
• Downloading / Installing Modules using NPM:
• Syntax to install any Node.js module −
$ npm install <Module Name>
• Example, following is the command to install package “upper-case”
$ npm install upper-case
• NPM creates a folder named "node_modules", where the package will be
placed.
• All packages you install in the future will be placed in this folder.
Using a Package

• Once the package is installed, it is ready to use.

• Include the "upper-case" package the same way you include any other
module:

• var uc = require('upper-case');
Example
• var http = require('http');
var uc = require('upper-case');
http.createServer(function (req, res) {
res.writeHead(200,
{'Content-Type': 'text/html'});
res.write(uc.upperCase("Hello World!"));
res.end();
}).listen(8080);
Global vs Local Installation

• By default, NPM installs any dependency in the local mode.


• Here local mode refers to the package installation in node_modules
directory lying in the folder where Node application is present.
• Locally deployed packages are accessible via require() method.
• For example, when we installed express module, it created
node_modules directory in the current directory where it installed the
express module.
• You can use npm ls command to list down all the locally installed modules.
• Open the Node.js command prompt and execute "npm ls“
• Globally installed packages/dependencies are stored in system directory.
• Such dependencies can be used in CLI (Command Line Interface) function of
any node.js but cannot be imported using require() in Node application
directly.
• Now let's try installing the express module using global installation.
$ npm install express -g
• This will produce a similar result but the module will be installed globally.
• Use the following command to check all the modules installed globally −
$ npm ls -g
Uninstalling a Module

• To uninstall a Node.js module, use the following command:


npm uninstall express
Updating a Package

• Update package.json and change the version of the dependency to be


updated and run the following command.
$ npm update express
What is the file `package.json`?
• JSON(Java Script Object Notation)
• The package.json file is the heart of Node.js system.
• It is the manifest file of any Node.js project and contains the metadata of the
project.
• The package.json file is the essential part to understand, learn and work with
the Node.js.
What does package.json file consist of?

• The metadata information in package.json file can be categorized into below


categories:
1. Identifying metadata properties: It basically consist of the properties to
identify the module/project such as the name of the project, current version
of the module, license, author of the project, description about the project
etc.
2. Functional metadata properties: As the name suggests, it consists of the
functional values/properties of the project/module such as the entry/starting
point of the module, dependencies in project, scripts being used, repository
links of Node project etc.
Who uses package.json file?

• NPM (Node Package Manager) uses this package.json file information


about Node JS Application information or Node JS Package details.
• package.json file contains a number of different directives or
elements. It uses these directives to tell NPM “How to handle the
module or package”.
• Configuration file in npm
Attributes of Package.json

• name − name of the package


• version − version of the package
• description − description of the package
• homepage − homepage of the package
• author − author of the package
• contributors − name of the contributors to the package
• dependencies − list of dependencies. NPM automatically installs all the
dependencies mentioned here in the node_module folder of the package.
• repository − repository type and URL of the package
• main − entry point of the package
• keywords − keywords
Example of sample package.json file
{
"name" : "sample",
"version" : "1.0.0"
}
Optional Directives
• Optional Directives
package.json file may contain many optional directives. We should use them
only if our Node JS package really requires. We will discuss some important
optional directives here.
• description: It is the description of the Node JS Project or module. We need
to provide brief and concise description about “What this module does”.
• dependencies: Every Node JS Project or Module may dependent on other
Node JS or Third Party Modules or our own Custom Modules. We should
provide all those dependencies by using this “dependencies” directive.
“dependencies”: {
<Define all your Module Dependencies here in key-value pairs>
}
• Here key is module name and value is required module version.
• For example;
"dependencies": {
"mypackage": "1.0.0",
"async": "0.8.0",
"express": "4.2.x“
}
Example of Complete Package
{
"name" : "sampleapp",
"version" : "1.0.0“,
"dependencies": {
"mypackage": "1.0.0",
"async": "0.8.0",
"express": "4.2.x" }
}
Here sampleapp is depending on 3 other Node JS Modules: mypackage (My
own Custom Module), async and express (Node JS Modules)
Example of Complete Package
{
"name" : "sampleapp",
"version" : "1.0.0“,
"dependencies": {
"mypackage": "1.0.0",
"async": "0.8.0",
"express": "4.2.x" }
}
Here sampleapp is depending on 3 other Node JS Modules: mypackage (My
own Custom Module), async and express (Node JS Modules)
Repository
• repository: This directive is used to specify the URL where your source code is live
for other people to contribute to your module or to learn your module. Its syntax is
shown below.
• The repository property of a package.json is an array that defines where the source
code for the module lives
• Inside your package.json, the repository property would look like this:

"repository": {
<Define your URL here>
}
Here we need to define two elements:
• Type of source to which you are going to provide URL
• url
Repository
• We define these two elements in key-value pairs. Here key is element
name and value is either type of source or url.
E.g.
"repository": {
"type" : "git",
"url" : "https://github.com/bnb/metaverse.git"
}
Creating a package.json file:

• A package.json file can be created in two ways:


1. Using npm init : Running this command, system expects user to fill the
vital information required as discussed above. It provides users with default
values which are editable by the user.
Syntax: npm init
2. Writing directly to file : One can directly write into file with all the required
information and can include it in the Node project.
Example: A demo package.json file with the required information.
{
"name": "GeeksForGeeks",
"version": "1.0.0",
"description": "GeeksForGeeks",
"repository": {
"type": "git",
"url": "https://github.com/gfg/gfg.git"
},
"dependencies": {
"mypackage": "1.0.0",
"async": "0.8.0",
"express": "4.2.x“
}
}

You might also like