user द्वारा create किये गए modules को ही local modules कहते हैं। आप अपनी need के according custom modules बनाकर उन्हें application में use कर सकते हैं।


custom modules बनाकर उसे आप के through distribute भी कर सकते हैं। जिससे Node.js community भी use कर सके। For Example आप database connection & data fetch करने के लिए module बनाकर उसे अपने application में use करने के अलावा distribute भी कर सकते हैं।

NodeJS Create Local Module

module create करने के लिए simply एक new file बनाएं और उसमे need के according code लिखे। जैसा की नीचे example में दिखाया गया है।

File : test_module.js

Copy
module.exports = function(name){
	console.log('Welcome : '+ name);
}
What is module.exports ?

NodeJS में module.exports एक predefined global Object है जो हर एक JavaScript file में present है। module एक simple variable है जो कि current file को represent करता है। NodeJS में module.exports के through ही module define करते हैं।
Read More About module.exports...

NodeJS Load Module

module load करने से मतलब है , file को include करना जिसमे आपने अभी code किया है। ध्यान रहे की आपने किस directory में module बनाया है , क्योंकि वही file name आपको require() function में pass करना होता है।


किसी भी module को load करने के लिए require function का use किया जाता है।

app.js

Copy
// now load module.
var test_module = require('./test_module');
// we will get function that is assigned to module.exports .
test_module('Node JS');

module load हो चुका है, file को run करने के लिए command prompt open करें और app.js file को locate करें।

C:\Users\Rahulkumar\Desktop\nodejs>node app.js
Welcome : Node JS

अगर module file किसी दूसरी directory के अंदर है तो उसके according path देते हैं।
For Example : require('./dir_name/module_name')

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers