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

PPTX
NodeJs Session03
PPTX
Nodejs Session01
PPTX
Activemq installation and master slave setup using shared broker data
PDF
Creating a Mesos python framework
PDF
Node JS | Dilkash Shaikh Mahajan
PPTX
Introduction to node.js
DOCX
Node js getting started
NodeJs Session03
Nodejs Session01
Activemq installation and master slave setup using shared broker data
Creating a Mesos python framework
Node JS | Dilkash Shaikh Mahajan
Introduction to node.js
Node js getting started

What's hot (18)

PPTX
Introduction to Node.js
PPTX
Kubernetes #3 security
ODP
Introduction to Mesos
PDF
Mesos introduction
PPTX
Node Session - 2
PPTX
บทที่ 3 การเขียนโปรแกรมติดต่อฐานข้อมูล
PDF
Getting Started Hacking OpenNebula - Fosdem-2013
PDF
Docker rant
PPTX
บทที่3
PDF
Configuring MongoDB HA Replica Set on AWS EC2
PDF
MySQL Proxy: Architecture and concepts of misuse
PPTX
Node Session - 4
PPTX
Nagios Conference 2014 - Jeff Mendoza - Monitoring Microsoft Azure with Nagios
PDF
Introduction to mesos bay
PPT
Aleksandr_Butenko_Mobile_Development
PPTX
Mule quartz
PDF
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
PPTX
Node js for beginners
Introduction to Node.js
Kubernetes #3 security
Introduction to Mesos
Mesos introduction
Node Session - 2
บทที่ 3 การเขียนโปรแกรมติดต่อฐานข้อมูล
Getting Started Hacking OpenNebula - Fosdem-2013
Docker rant
บทที่3
Configuring MongoDB HA Replica Set on AWS EC2
MySQL Proxy: Architecture and concepts of misuse
Node Session - 4
Nagios Conference 2014 - Jeff Mendoza - Monitoring Microsoft Azure with Nagios
Introduction to mesos bay
Aleksandr_Butenko_Mobile_Development
Mule quartz
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
Node js for beginners
Ad

Similar to NodeJs Session02 (20)

PDF
NodeJs Modules1.pdf
PPTX
node.js.pptx
PDF
Advanced Node.JS Meetup
DOCX
unit 2 of Full stack web development subject
PDF
Global objects in Node.pdf
PPTX
Requiring your own files.pptx
PDF
UNIT-3.pdf, buffer module, treams,file accessing using node js
PPTX
Nodejs functions & modules
PPTX
What's New in Java 9
PDF
NodeJS: an Introduction
PPTX
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
PDF
Node Web Development 2nd Edition: Chapter3 Node Modules
PPTX
Introduction to Webpack 5.0 Presentation
PPTX
React Basic and Advance || React Basic
PDF
Warsaw Frontend Meetup #1 - Webpack
PPT
Node js Modules and Event Emitters
PPTX
Introduction to Node.js
PPTX
Packing for the Web with Webpack
PPTX
JavaScript Module Loaders
NodeJs Modules1.pdf
node.js.pptx
Advanced Node.JS Meetup
unit 2 of Full stack web development subject
Global objects in Node.pdf
Requiring your own files.pptx
UNIT-3.pdf, buffer module, treams,file accessing using node js
Nodejs functions & modules
What's New in Java 9
NodeJS: an Introduction
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
Node Web Development 2nd Edition: Chapter3 Node Modules
Introduction to Webpack 5.0 Presentation
React Basic and Advance || React Basic
Warsaw Frontend Meetup #1 - Webpack
Node js Modules and Event Emitters
Introduction to Node.js
Packing for the Web with Webpack
JavaScript Module Loaders
Ad

More from Jainul Musani (20)

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

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Mobile App Security Testing_ A Comprehensive Guide.pdf

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;