Professional Documents
Culture Documents
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
Coursary Open
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;
To get the average of any two numbers, we can pass the values 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"
}
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";
console.log(mod.name)
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.
// 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");
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