SlideShare a Scribd company logo
Workshop
@
Ramnshee Infotech
By Jainul Musani
SESSION:02
2
Node.js Package Manager - NPM
 Node Package Manager provides two main
functionalities:
 It provides online repositories for node.js
packages/modules which are searchable on
search.nodejs.org
 It also provides command line utility to install
Node.js packages, do version management and
dependency management of Node.js packages.
SESSION:01
Node.js Package Manager - NPM
 The npm comes bundled with Node.js installables in versions
after that v0.6.3. You can check the version by opening
Node.js command prompt and typing the following
command:
> npm version
C:UsersJamtemp>npm version
{
npm: '6.14.4',
ares: '1.16.0',
brotli: '1.0.7',
cldr: '37.0',
SESSION:02
http_parser: '2.9.3',
icu: '67.1',
llhttp: '2.0.4',
modules: '72',
napi: '6',
nghttp2: '1.40.0',
node: '12.17.0',
openssl: '1.1.1g',
tz: '2019c',
unicode: '13.0',
uv: '1.37.0',
v8: '7.8.279.23-node.37',
Installing Modules using npm
 Syntax:
npm install <Module Name>
Example:
npm install express
 Global vs Local Installation
npm install express -g
 Uninstalling a Module
npm uninstall express
SESSION:02
Searching a modules using npm
 Syntax:
npm search <Module Name>
Example:
npm search express
SESSION:02
Node.js Module Types
 Node.js includes three types of modules:
1. Core Modules
2. Local Modules
3. Third Party Modules
SESSION:02
Node.js Core Modules
 Node.js is a light weight framework.
 The core modules include bare minimum
functionalities of Node.js.
 These core modules are compiled into its binary
distribution and load automatically when Node.js
process starts.
 You need to import the core modules first in order
to use it in your application.
SESSION:02
Node.js Core Modules
Core Module Description
http http module includes classes, methods and events to create
Node.js http server.
url url module includes methods for URL resolution and parsing.
querystring querystring module includes methods to deal with query
string.
path path module includes methods to deal with file paths.
fs fs module includes classes, methods, and events to work
with file I/O.
util util module includes utility functions useful for
programmers.
SESSION:02
Node.js Core Modules
SESSION:02
 Loading Core Modules
In order to use Node.js core or NPM modules, you first need to
import it using require() function as shown below.
var module = require('module_name');
As per above syntax, specify the module name in the require()
function.
The require() function will return an object, function, property or
any other JavaScript type, depending on what the specified module
returns.
Node.js Core Modules
SESSION:02
 The following example demonstrates how to use Node.js
http module to create a web server.
var http = require('http');
var server =
http.createServer(function(req, res)
{
res.writeHead(200,{'Content-Type': 'text/html'});
res.write('Node.js says hello!');
res.end();
});
server.listen(5000);
Node.js Local Modules
SESSION:02
 Local modules are modules created locally in your
Node.js application.
 These modules include different functionalities of your
application in separate files and folders.
 You can also package it and distribute it via NPM, so that
Node.js community can use it.
 For example, if you need to connect to MongoDB and
fetch data then you can create a module for it, which
can be reused in your application.
Writing Simple Module
SESSION:02
var greet= {
morning: function (msg) {
console.log(‘Good Morning: Mr. ' + msg);
},
afternoon:function (msg) {
console.log(‘Good Afternoon Mr. ' + msg);
},
evening:function (msg) {
console.log(‘Good Evening Mr. ' + msg);
}
};
module.exports = greet
Node.js Local Modules
SESSION:02
 The module.exports is a special object which is
included in every JS file in the Node.js application by
default.
 Use module.exports or exports to expose a function,
object or variable as a module in Node.js.
app.js
var mygreet = require('./greet.js');
mygreet.morning(‘Sachin');
Export Module in Node.js
SESSION:02
 The module.exports is a special object which is
included in every JavaScript file in the Node.js
application by default.
 The module is a variable that represents the
current module, and exports is an object that
will be exposed as a module.
 So, whatever you assign to module. exports will
be exposed as a module.
Export Literals
SESSION:02
 As exports is an object. It exposes whatever you
assigned to it as a module.
 For example, if you assign a string literal then it
will expose that string literal as a module.
Export Object
SESSION:02
 The exports is an object.
 So, you can attach properties or methods to it.
Export Object
SESSION:02
Export Function
SESSION:02
Export Function as a Class
SESSION:02
Load Module from a Separate Folder
SESSION:02
Use the full path of a module file where you have
exported it using module.exports.
For example, if the mylib module in the Jlib.js is
stored under the utility folder under the root folder
of your application, then import it, as shown below.
var mylib = require('./utility/Jlib.js');
Load Module from a Separate Folder
SESSION:02
example:
Create a calculator.js in ‘temp’ folder
// Returns addition of two numbers
exports.add = function (a, b) {
return a+b;
};
exports.subtract = function (a, b) {
return a-b;
};
exports.multiply = function (a, b) {
return a*b;
};
Load Module from a Separate Folder
SESSION:02
example:
Create a moduleExample.js outside ‘temp’ folder
var calculator = require('./temp/calculator');
var a=10, b=5;
console.log("Addition : "+calculator.add(a,b));
console.log("Subtraction : "+calculator.subtract(a,b));
console.log("Multiplication : "+calculator.multiply(a,b));
How to extend the existing module
SESSION:02
Extend or add functions to Node.js module
Steps:
1. Include the module
2. Add function to the module variable
3. Re-export the module
Example: Creating a new .js file myExt.js
var calc = require(‘./calculator');
calc.divide = function(a,b){
return a/b;
};
module.exports = calc;
Override function of a Node.js module
SESSION:02
Extend or add functions to Node.js module
Steps:
1. Include the module
2. Delete function from the module variable.
3. Add the function with the same name to the module variable
4. Re-export the module
Example: overriding a function in the file myExt.js
var calc = require(‘./calculator');
delete calc[‘divide’];
calc.divide = function(a,b){
console.log(‘New Func Added’);
return a/b;
};
module.exports = calc;

More Related Content

What's hot (18)

PPTX
Introduction to Node.js
Winston Hsieh
 
PPTX
Kubernetes #3 security
Terry Cho
 
ODP
Introduction to Mesos
koboltmarky
 
PDF
Mesos introduction
Olivier Sallou
 
PPTX
Node Session - 2
Bhavin Shah
 
PPTX
บทที่ 3 การเขียนโปรแกรมติดต่อฐานข้อมูล
Priew Chakrit
 
PDF
Getting Started Hacking OpenNebula - Fosdem-2013
OpenNebula Project
 
PDF
Docker rant
gnosek
 
PPTX
บทที่3
Waritsara Sonchan
 
PDF
Configuring MongoDB HA Replica Set on AWS EC2
ShepHertz
 
PDF
MySQL Proxy: Architecture and concepts of misuse
weigon
 
PPTX
Node Session - 4
Bhavin Shah
 
PPTX
Nagios Conference 2014 - Jeff Mendoza - Monitoring Microsoft Azure with Nagios
Nagios
 
PDF
Introduction to mesos bay
hongbin034
 
PPT
Aleksandr_Butenko_Mobile_Development
Ciklum
 
PPTX
Mule quartz
Praneethchampion
 
PDF
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
Tom Croucher
 
PPTX
Node js for beginners
Arjun Sreekumar
 
Introduction to Node.js
Winston Hsieh
 
Kubernetes #3 security
Terry Cho
 
Introduction to Mesos
koboltmarky
 
Mesos introduction
Olivier Sallou
 
Node Session - 2
Bhavin Shah
 
บทที่ 3 การเขียนโปรแกรมติดต่อฐานข้อมูล
Priew Chakrit
 
Getting Started Hacking OpenNebula - Fosdem-2013
OpenNebula Project
 
Docker rant
gnosek
 
บทที่3
Waritsara Sonchan
 
Configuring MongoDB HA Replica Set on AWS EC2
ShepHertz
 
MySQL Proxy: Architecture and concepts of misuse
weigon
 
Node Session - 4
Bhavin Shah
 
Nagios Conference 2014 - Jeff Mendoza - Monitoring Microsoft Azure with Nagios
Nagios
 
Introduction to mesos bay
hongbin034
 
Aleksandr_Butenko_Mobile_Development
Ciklum
 
Mule quartz
Praneethchampion
 
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
Tom Croucher
 
Node js for beginners
Arjun Sreekumar
 

Similar to NodeJs Session02 (20)

PDF
NodeJs Modules1.pdf
Bareen Shaikh
 
PPTX
node.js.pptx
rani marri
 
PDF
Advanced Node.JS Meetup
LINAGORA
 
DOCX
unit 2 of Full stack web development subject
JeneferAlan1
 
PDF
Global objects in Node.pdf
SudhanshiBakre1
 
PPTX
Requiring your own files.pptx
Lovely Professional University
 
PDF
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
PPTX
Nodejs functions & modules
monikadeshmane
 
PPTX
What's New in Java 9
Richard Langlois P. Eng.
 
PDF
NodeJS: an Introduction
Roberto Casadei
 
PPTX
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
vinodkumarthatipamul
 
PDF
Node Web Development 2nd Edition: Chapter3 Node Modules
Rick Chang
 
PPTX
Introduction to Webpack 5.0 Presentation
Knoldus Inc.
 
PPTX
React Basic and Advance || React Basic
rafaqathussainc077
 
PDF
Warsaw Frontend Meetup #1 - Webpack
Radosław Rosłaniec
 
PPT
Node js Modules and Event Emitters
TheCreativedev Blog
 
PPTX
Introduction to Node.js
Vikash Singh
 
PPTX
Packing for the Web with Webpack
Thiago Temple
 
PPTX
JavaScript Module Loaders
zeroproductionincidents
 
NodeJs Modules1.pdf
Bareen Shaikh
 
node.js.pptx
rani marri
 
Advanced Node.JS Meetup
LINAGORA
 
unit 2 of Full stack web development subject
JeneferAlan1
 
Global objects in Node.pdf
SudhanshiBakre1
 
Requiring your own files.pptx
Lovely Professional University
 
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
Nodejs functions & modules
monikadeshmane
 
What's New in Java 9
Richard Langlois P. Eng.
 
NodeJS: an Introduction
Roberto Casadei
 
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
vinodkumarthatipamul
 
Node Web Development 2nd Edition: Chapter3 Node Modules
Rick Chang
 
Introduction to Webpack 5.0 Presentation
Knoldus Inc.
 
React Basic and Advance || React Basic
rafaqathussainc077
 
Warsaw Frontend Meetup #1 - Webpack
Radosław Rosłaniec
 
Node js Modules and Event Emitters
TheCreativedev Blog
 
Introduction to Node.js
Vikash Singh
 
Packing for the Web with Webpack
Thiago Temple
 
JavaScript Module Loaders
zeroproductionincidents
 
Ad

More from Jainul Musani (20)

PDF
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
PDF
Java Abstract and Final Class for BCA students
Jainul Musani
 
PDF
Java Collection Framework for BCA Students
Jainul Musani
 
PDF
Simple Calculator using JavaFx a part of Advance Java
Jainul Musani
 
PDF
JavaFx Introduction, Basic JavaFx Architecture
Jainul Musani
 
PDF
ASP.NET 2010, WebServices Full Example for BCA Students
Jainul Musani
 
PDF
Palindrome Programme in PHP for BCA students
Jainul Musani
 
PDF
Leap Year Program in PHP for BCA students
Jainul Musani
 
PDF
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
PDF
Python: The Versatile Programming Language - Introduction
Jainul Musani
 
PPTX
Python a Versatile Programming Language - Introduction
Jainul Musani
 
PDF
React js t8 - inlinecss
Jainul Musani
 
PDF
React js t7 - forms-events
Jainul Musani
 
PDF
React js t6 -lifecycle
Jainul Musani
 
PDF
React js t5 - state
Jainul Musani
 
PDF
React js t4 - components
Jainul Musani
 
PDF
React js t3 - es6
Jainul Musani
 
PDF
React js t2 - jsx
Jainul Musani
 
PDF
React js t1 - introduction
Jainul Musani
 
PPTX
ExpressJs Session01
Jainul Musani
 
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
Java Abstract and Final Class for BCA students
Jainul Musani
 
Java Collection Framework for BCA Students
Jainul Musani
 
Simple Calculator using JavaFx a part of Advance Java
Jainul Musani
 
JavaFx Introduction, Basic JavaFx Architecture
Jainul Musani
 
ASP.NET 2010, WebServices Full Example for BCA Students
Jainul Musani
 
Palindrome Programme in PHP for BCA students
Jainul Musani
 
Leap Year Program in PHP for BCA students
Jainul Musani
 
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Python: The Versatile Programming Language - Introduction
Jainul Musani
 
Python a Versatile Programming Language - Introduction
Jainul Musani
 
React js t8 - inlinecss
Jainul Musani
 
React js t7 - forms-events
Jainul Musani
 
React js t6 -lifecycle
Jainul Musani
 
React js t5 - state
Jainul Musani
 
React js t4 - components
Jainul Musani
 
React js t3 - es6
Jainul Musani
 
React js t2 - jsx
Jainul Musani
 
React js t1 - introduction
Jainul Musani
 
ExpressJs Session01
Jainul Musani
 
Ad

Recently uploaded (20)

PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
July Patch Tuesday
Ivanti
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 

NodeJs Session02

  • 2. Node.js Package Manager - NPM  Node Package Manager provides two main functionalities:  It provides online repositories for node.js packages/modules which are searchable on search.nodejs.org  It also provides command line utility to install Node.js packages, do version management and dependency management of Node.js packages. SESSION:01
  • 3. Node.js Package Manager - NPM  The npm comes bundled with Node.js installables in versions after that v0.6.3. You can check the version by opening Node.js command prompt and typing the following command: > npm version C:UsersJamtemp>npm version { npm: '6.14.4', ares: '1.16.0', brotli: '1.0.7', cldr: '37.0', SESSION:02 http_parser: '2.9.3', icu: '67.1', llhttp: '2.0.4', modules: '72', napi: '6', nghttp2: '1.40.0', node: '12.17.0', openssl: '1.1.1g', tz: '2019c', unicode: '13.0', uv: '1.37.0', v8: '7.8.279.23-node.37',
  • 4. Installing Modules using npm  Syntax: npm install <Module Name> Example: npm install express  Global vs Local Installation npm install express -g  Uninstalling a Module npm uninstall express SESSION:02
  • 5. Searching a modules using npm  Syntax: npm search <Module Name> Example: npm search express SESSION:02
  • 6. Node.js Module Types  Node.js includes three types of modules: 1. Core Modules 2. Local Modules 3. Third Party Modules SESSION:02
  • 7. Node.js Core Modules  Node.js is a light weight framework.  The core modules include bare minimum functionalities of Node.js.  These core modules are compiled into its binary distribution and load automatically when Node.js process starts.  You need to import the core modules first in order to use it in your application. SESSION:02
  • 8. Node.js Core Modules Core Module Description http http module includes classes, methods and events to create Node.js http server. url url module includes methods for URL resolution and parsing. querystring querystring module includes methods to deal with query string. path path module includes methods to deal with file paths. fs fs module includes classes, methods, and events to work with file I/O. util util module includes utility functions useful for programmers. SESSION:02
  • 9. Node.js Core Modules SESSION:02  Loading Core Modules In order to use Node.js core or NPM modules, you first need to import it using require() function as shown below. var module = require('module_name'); As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.
  • 10. Node.js Core Modules SESSION:02  The following example demonstrates how to use Node.js http module to create a web server. var http = require('http'); var server = http.createServer(function(req, res) { res.writeHead(200,{'Content-Type': 'text/html'}); res.write('Node.js says hello!'); res.end(); }); server.listen(5000);
  • 11. Node.js Local Modules SESSION:02  Local modules are modules created locally in your Node.js application.  These modules include different functionalities of your application in separate files and folders.  You can also package it and distribute it via NPM, so that Node.js community can use it.  For example, if you need to connect to MongoDB and fetch data then you can create a module for it, which can be reused in your application.
  • 12. Writing Simple Module SESSION:02 var greet= { morning: function (msg) { console.log(‘Good Morning: Mr. ' + msg); }, afternoon:function (msg) { console.log(‘Good Afternoon Mr. ' + msg); }, evening:function (msg) { console.log(‘Good Evening Mr. ' + msg); } }; module.exports = greet
  • 13. Node.js Local Modules SESSION:02  The module.exports is a special object which is included in every JS file in the Node.js application by default.  Use module.exports or exports to expose a function, object or variable as a module in Node.js. app.js var mygreet = require('./greet.js'); mygreet.morning(‘Sachin');
  • 14. Export Module in Node.js SESSION:02  The module.exports is a special object which is included in every JavaScript file in the Node.js application by default.  The module is a variable that represents the current module, and exports is an object that will be exposed as a module.  So, whatever you assign to module. exports will be exposed as a module.
  • 15. Export Literals SESSION:02  As exports is an object. It exposes whatever you assigned to it as a module.  For example, if you assign a string literal then it will expose that string literal as a module.
  • 16. Export Object SESSION:02  The exports is an object.  So, you can attach properties or methods to it.
  • 19. Export Function as a Class SESSION:02
  • 20. Load Module from a Separate Folder SESSION:02 Use the full path of a module file where you have exported it using module.exports. For example, if the mylib module in the Jlib.js is stored under the utility folder under the root folder of your application, then import it, as shown below. var mylib = require('./utility/Jlib.js');
  • 21. Load Module from a Separate Folder SESSION:02 example: Create a calculator.js in ‘temp’ folder // Returns addition of two numbers exports.add = function (a, b) { return a+b; }; exports.subtract = function (a, b) { return a-b; }; exports.multiply = function (a, b) { return a*b; };
  • 22. Load Module from a Separate Folder SESSION:02 example: Create a moduleExample.js outside ‘temp’ folder var calculator = require('./temp/calculator'); var a=10, b=5; console.log("Addition : "+calculator.add(a,b)); console.log("Subtraction : "+calculator.subtract(a,b)); console.log("Multiplication : "+calculator.multiply(a,b));
  • 23. How to extend the existing module SESSION:02 Extend or add functions to Node.js module Steps: 1. Include the module 2. Add function to the module variable 3. Re-export the module Example: Creating a new .js file myExt.js var calc = require(‘./calculator'); calc.divide = function(a,b){ return a/b; }; module.exports = calc;
  • 24. Override function of a Node.js module SESSION:02 Extend or add functions to Node.js module Steps: 1. Include the module 2. Delete function from the module variable. 3. Add the function with the same name to the module variable 4. Re-export the module Example: overriding a function in the file myExt.js var calc = require(‘./calculator'); delete calc[‘divide’]; calc.divide = function(a,b){ console.log(‘New Func Added’); return a/b; }; module.exports = calc;