You are on page 1of 4

3/27/2021 Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web Development Tutorials #68 - Code With

eJs | Web Development Tutorials #68 - Code With Harry

Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web Development Tutorials #68

Course Content
Hide Player
p
Navigation The DOM | Web
Development Tutorials #56
 Free YouTube Video

57. JavaScript Tutorial: Events


& Listening to Events | Web
Development Tutorials #57
 Free YouTube Video

58. JavaScript Tutorial:


setInterval & setTimeOut |
Overview Q&A Files Announcements Web Development Tutorials
#58

Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web


 Free YouTube Video

Development Tutorials #68


59. JavaScript Tutorial: Date &
In this tutorial, we are going to learn how to create our own custom modules in Node using NodeJs. The purpose
Time InofJavaScript
creating our own
| Web
modules is to help other creators to use them in their code which can help them. Development Tutorials #59
 Free YouTube Video
We can start by making a new folder as tut68. Inside the folder, we will make two files as index.js and mod.js. Inside index.js, we will make a
file that will use modules, and inside mod.js, we will make our own modules.
60. JavaScript Tutorial: Arrow
Functions In JavaScript | Web
Development Tutorials #60
 Free YouTube Video
Find Web Design Courses

61. JavaScript Tutorial: Math


Free comparison tool for nding Web Design courses online.
Object In JavaScript | Web
Development Tutorials #61
 Free YouTube Video

Coursary Open

62. JavaScript Tutorial: Working


We will start by making the module first and the code for it goes as follows-
with JSON in JavaScript | Web
Development Tutorials #62
function average(arr){  Free YouTube Video
let sum = 0;
arr.forEach(element => {
63. Backend Tutorial: Node.Js
sum += element;
Introduction and Installation |
});
Web Development Tutorials
return sum/arr.length; #63
}  Free YouTube Video

In the above example, we have created a simple average function that takes the value as arr and returns the average.
64. Backend Tutorial: Node.Js
Now to import this module, we have to write as follows in index.js- Modules with Examples | Web
Development Tutorials #64
https://www.codewithharry.com/videos/web-development-in-hindi-68 1/5
3/27/2021 Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web Development Tutorials #68 - Code With Harry
p
 Free YouTube Video
const mod = require("./mod");
console.log(mod.name)

After executing this, one thing to notice is, the code gets executed but we still do not get the function back. To get back the function, we
have to make the function to behave explicitly in mod.js file. We can do this as follows-

module.exports = average;

Now when we run the function, we get it back as follows-

To get the average of any two numbers, we can pass the values as follows-

const average = require("./mod");


console.log(average([3,4])

When we run the function now, we get the output as follows-

https://www.codewithharry.com/videos/web-development-in-hindi-68 2/5
3/27/2021 Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web Development Tutorials #68 - Code With Harry

We can also create an object and make it as a module. For example, we can write as follows-

module.exports = {
avg: average,
name: "Harry",
repo: "GitHub"
}

We can call this module in index.js file as follows-

const mod = require("./mod");


console.log(mod.avg([3,4]))

In the same way, we can export multiple objects and create our own modules with the help of NodeJs. However, there is another method to
call the above object. If we write as follows in mod.js file-

module.exports.name = "Harry";

And call the object in the following way in index.js as follows-

console.log(mod.name)

We get the output as “Harry” as follows-

https://www.codewithharry.com/videos/web-development-in-hindi-68 3/5
3/27/2021 Backend Tutorial: Creating Custom Modules in Node Using NodeJs | Web Development Tutorials #68 - Code With Harry

In this way, NodeJs help us to create different other modules to make our work easier. I hope you must have got an idea of how to create
custom modules. You can try to create different other modules and practice more.

Code index.js as described/written in the video

// console.log(average([3,4]))
// const average = require("./mod");
const mod = require("./mod");
console.log(mod.name)
// console.log(mod.avg([3,4]))
console.log("This is index.js");

Code mod.js as described/written in the video

console.log("This is module");

function average(arr){
let sum = 0;
arr.forEach(element => {
sum += element;
});
return sum/arr.length;
}

// module.exports = {
// avg: average,
// name: "Harry",
// repo: "GitHub"
// }

module.exports.name = "Harry";

← Previous Next →
https://www.codewithharry.com/videos/web-development-in-hindi-68 4/5

You might also like