SlideShare a Scribd company logo
Prepared By:
Prof. Bareen Shaikh
Department of Computer
Science,
MIT ACSC,ALNDI(D), PUNE
NodeJs Modules
Topics
 3.3 Module and ModuleTypes
 3.4 Core Module, Local Module
 3.5 Directories as module
 3.5 Module. exports
Module and Module Types
 Module in Node.js is a simple or complex functionality organized in
single or multiple JavaScript files which can be reused throughout the
Node.js application.
 Each module in Node.js has its own context, so it cannot interfere with
other modules or pollute global scope.
 Each module can be placed in a separate .js file under a separate folder.
 Node.js ModuleTypes: Node.js includes three types of modules:
1. Core Modules
2. Local Modules
Core Module
 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.
 Need to be import the core module first in order to use in an
application.
 Following lists some of the important core modules in Node.js
 http
 url
 util
 path
 fs
Loading Core Module
 In order to use Node.js core or NPM modules, need to import it
using require() function as shown below.
var module = require('module_name');
 require() function return an object, function, property or any other
JavaScript type, depending on what the specified module returns.
Eg.
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
}); server.listen(5000);
Local Module
 Local modules are modules created locally by user in Node.js
application.
 Modules may include different functionalities of application in
separate files and folders.
 These module can also package it and distribute it via NPM, so that
Node.js community can use it.
 For example,
If an application need to connect to MongoDB and fetch data then user
can create a module for it, which can be reused in many application that
are requiring it.
Creating Local Module
 File name suppose module_calc.js which consists of following code.
function add(a,b){
return(a+b)
}
module.exports=add
 Create another file calculator.js which consists of following code.
var req=require('./module_cal.js');
result=req(5,4)
console.log("output is"+result);
Export Module
 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 assign to module.exports will be exposed as a module.
 Different types as a module using module.exports
 Exports Literals
 Export Object
 Export Function
Export Literals
 Assigning a string literal then it will expose that string literal as a
module.
 Suppose following code is written in mod.js file
module.exports = 'Hello world‘;
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg);
Export Object
 Exports is an object. So, can attach properties or methods to it.
 Suppose following code is written in mod.js file
exports.SimpleMessage = 'Hello world';
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg.SimpleMessage);
Attach Object to module.exports
 Suppose following code is written in mod.js file
module.exports = {
firstName:‘Bareen',
lastName:‘Shaikh’
}
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg.firstName + ' ' + msg.lastName);
Export Function
 Can attach an anonymous function to exports object as shown below
 Suppose following code is written in mod.js file
module.exports = function (msg) {
console.log(msg);
};
 In another app.js file following code is written
var msg = require('./mod.js');
msg('HelloWorld');
Export Function as Class
 In JavaScript, a function can be treated like a class.
 Suppose following code is written in mod.js file
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
}
}
 In another app.js file following code is written
var req= require('./mod.js');
var person = new req(‘Bareen',‘Shaikh');
console.log(person.fullName());
THANK YOU.

More Related Content

Similar to NodeJs Modules1.pdf

Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
LINAGORA
 
What's New in Java 9
What's New in Java 9What's New in Java 9
What's New in Java 9
Richard Langlois P. Eng.
 
Java9
Java9Java9
Java 9 Module System
Java 9 Module SystemJava 9 Module System
Java 9 Module System
Hasan Ünal
 
Modular javascript
Modular javascriptModular javascript
Modular javascript
Zain Shaikh
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basic
rafaqathussainc077
 
Utility Modules in Node.js.pdf
Utility Modules in Node.js.pdfUtility Modules in Node.js.pdf
Utility Modules in Node.js.pdf
SudhanshiBakre1
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
James Carr
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
Brian Cavalier
 
NodeJS: an Introduction
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an Introduction
Roberto Casadei
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
Matteo Manchi
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
Radosław Rosłaniec
 
Frankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGiFrankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGi
Toni Epple
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Packing for the Web with Webpack
Packing for the Web with WebpackPacking for the Web with Webpack
Packing for the Web with Webpack
Thiago Temple
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
Claudio Procida
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
Jonathan Fine
 
Introduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 PresentationIntroduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 Presentation
Knoldus Inc.
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
Akhil Mittal
 

Similar to NodeJs Modules1.pdf (20)

Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
What's New in Java 9
What's New in Java 9What's New in Java 9
What's New in Java 9
 
Java9
Java9Java9
Java9
 
Java 9 Module System
Java 9 Module SystemJava 9 Module System
Java 9 Module System
 
Modular javascript
Modular javascriptModular javascript
Modular javascript
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basic
 
Utility Modules in Node.js.pdf
Utility Modules in Node.js.pdfUtility Modules in Node.js.pdf
Utility Modules in Node.js.pdf
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
 
NodeJS: an Introduction
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an Introduction
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
 
Frankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGiFrankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGi
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Packing for the Web with Webpack
Packing for the Web with WebpackPacking for the Web with Webpack
Packing for the Web with Webpack
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Introduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 PresentationIntroduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 Presentation
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 

More from Bareen Shaikh

Express Generator.pdf
Express Generator.pdfExpress Generator.pdf
Express Generator.pdf
Bareen Shaikh
 
Middleware.pdf
Middleware.pdfMiddleware.pdf
Middleware.pdf
Bareen Shaikh
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
Bareen Shaikh
 
Express JS-Routingmethod.pdf
Express JS-Routingmethod.pdfExpress JS-Routingmethod.pdf
Express JS-Routingmethod.pdf
Bareen Shaikh
 
FS_module_functions.pptx
FS_module_functions.pptxFS_module_functions.pptx
FS_module_functions.pptx
Bareen Shaikh
 
File System.pptx
File System.pptxFile System.pptx
File System.pptx
Bareen Shaikh
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
Bareen Shaikh
 
NPM.pdf
NPM.pdfNPM.pdf
NPM.pdf
Bareen Shaikh
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
Bareen Shaikh
 
Introduction to Node JS1.pdf
Introduction to Node JS1.pdfIntroduction to Node JS1.pdf
Introduction to Node JS1.pdf
Bareen Shaikh
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
Bareen Shaikh
 

More from Bareen Shaikh (11)

Express Generator.pdf
Express Generator.pdfExpress Generator.pdf
Express Generator.pdf
 
Middleware.pdf
Middleware.pdfMiddleware.pdf
Middleware.pdf
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
 
Express JS-Routingmethod.pdf
Express JS-Routingmethod.pdfExpress JS-Routingmethod.pdf
Express JS-Routingmethod.pdf
 
FS_module_functions.pptx
FS_module_functions.pptxFS_module_functions.pptx
FS_module_functions.pptx
 
File System.pptx
File System.pptxFile System.pptx
File System.pptx
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
NPM.pdf
NPM.pdfNPM.pdf
NPM.pdf
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Introduction to Node JS1.pdf
Introduction to Node JS1.pdfIntroduction to Node JS1.pdf
Introduction to Node JS1.pdf
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 

Recently uploaded

Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

NodeJs Modules1.pdf

  • 1. Prepared By: Prof. Bareen Shaikh Department of Computer Science, MIT ACSC,ALNDI(D), PUNE NodeJs Modules
  • 2. Topics  3.3 Module and ModuleTypes  3.4 Core Module, Local Module  3.5 Directories as module  3.5 Module. exports
  • 3. Module and Module Types  Module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.  Each module in Node.js has its own context, so it cannot interfere with other modules or pollute global scope.  Each module can be placed in a separate .js file under a separate folder.  Node.js ModuleTypes: Node.js includes three types of modules: 1. Core Modules 2. Local Modules
  • 4. Core Module  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.  Need to be import the core module first in order to use in an application.  Following lists some of the important core modules in Node.js  http  url  util  path  fs
  • 5. Loading Core Module  In order to use Node.js core or NPM modules, need to import it using require() function as shown below. var module = require('module_name');  require() function return an object, function, property or any other JavaScript type, depending on what the specified module returns. Eg. var http = require('http'); var server = http.createServer(function(req, res){ //write code here }); server.listen(5000);
  • 6. Local Module  Local modules are modules created locally by user in Node.js application.  Modules may include different functionalities of application in separate files and folders.  These module can also package it and distribute it via NPM, so that Node.js community can use it.  For example, If an application need to connect to MongoDB and fetch data then user can create a module for it, which can be reused in many application that are requiring it.
  • 7. Creating Local Module  File name suppose module_calc.js which consists of following code. function add(a,b){ return(a+b) } module.exports=add  Create another file calculator.js which consists of following code. var req=require('./module_cal.js'); result=req(5,4) console.log("output is"+result);
  • 8. Export Module  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 assign to module.exports will be exposed as a module.  Different types as a module using module.exports  Exports Literals  Export Object  Export Function
  • 9. Export Literals  Assigning a string literal then it will expose that string literal as a module.  Suppose following code is written in mod.js file module.exports = 'Hello world‘;  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg);
  • 10. Export Object  Exports is an object. So, can attach properties or methods to it.  Suppose following code is written in mod.js file exports.SimpleMessage = 'Hello world';  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg.SimpleMessage);
  • 11. Attach Object to module.exports  Suppose following code is written in mod.js file module.exports = { firstName:‘Bareen', lastName:‘Shaikh’ }  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg.firstName + ' ' + msg.lastName);
  • 12. Export Function  Can attach an anonymous function to exports object as shown below  Suppose following code is written in mod.js file module.exports = function (msg) { console.log(msg); };  In another app.js file following code is written var msg = require('./mod.js'); msg('HelloWorld');
  • 13. Export Function as Class  In JavaScript, a function can be treated like a class.  Suppose following code is written in mod.js file module.exports = function (firstName, lastName) { this.firstName = firstName; this.lastName = lastName; this.fullName = function () { return this.firstName + ' ' + this.lastName; } }  In another app.js file following code is written var req= require('./mod.js'); var person = new req(‘Bareen',‘Shaikh'); console.log(person.fullName());