पिछले topic में आपने पढ़ा कि NodeJS में कैसे module को create करते हैं और कैसे उसे use करते हैं। अगर आपको याद है तो module define करने के लिए एक statement use किया था - module.exports . इस topic में आप पढ़ेंगे कि module.exports है क्या ? और क्या इसके अलावा दुसरे तरीके से भी किसी module को load कर सकते हैं।

What is module.exports ?

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


तो जब भी file में module define करते हैं तो हमें module.exports में एक value assign करनी पड़ती है। value कुछ भी हो सकती है जैसे literals , number , function , object , class etc . जो भी आप assign करोगे , module include करने पर आपको वही return में मिलेगा।
For Example :

File : test_module.js

Copy
// use function as a class
module.exports = function user(){
	this.username = null;
	this.set_name = function(fullname){
		this.username = fullname;
		console.log('username set successfully !')
	}
	this.get_name = function(){
		return this.username;
	}
}

File : app.js

Copy
var user_class = require('./test_module');
// here we will get function as a class.
var user = new user_class();
user.set_name('Rahul Kumar');
var username = user.get_name();
console.log('Hello : '+ username);
C:\Users\HP\Desktop\workspace\nodejs>node app.js
username set successfully !
Hello : Rahul Kumar

तो कुछ तरह से module.exports work करता है , जो अभी आप assign करेंगे वो ही आपको मिलेगा।

Important
याद रहे कि module में module.exports हमेशा last में assign की गयी value return करता है।

module.exports = 'first value';
module.exports = 'last value';

ऊपर दिए गए module को include करने पर last value ही मिलेगी।

Module Without exports

अभी तक जो भी modules define किये गए थे सभी module.exports का use करके बनाये थे , लेकिन हम बिना module variable का use किये भी module बना सकते हैं। फिर directly exports में कोई भी property / key add करके value assign कर सकते हैं। और module include करते समय same key / property name का use करना पड़ता है।

Note : जब हम बिना module variable का use करके , directly exports Object में properties = value assign करते हैं तो हम multiple values assign कर सकते हैं। जैसा कि नीचे example में देख सकते हैं।

See Example :

File : cars.js

Copy
// we can also do it without exports object.
exports.tesla = {
     drive : function(){
          console.log('I am driving Tesla..');
     }
};


// In this case we can assign multiple values.
exports.jaguar = {
     drive : function(){
          console.log('I am driving Jaguar..');
     }
};

File : app.js

Copy
var car_module = require('./cars');
// here we have to access with the same key.
var tesla = car_module.tesla;
tesla.drive();

var jaguar = car_module.jaguar;
jaguar.drive();
C:\Users\HP\Desktop\workspace\nodejs>node app.js
I am driving Tesla..
I am driving Jaguar..

I Hope, अब आप module.exports के बारे में अच्छे से समझ गए होंगे।

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