[0:00]Hey everyone, welcome back. And welcome to another video on Node JS tutorial series. Aur is video mein hum dekhne wale hain what is modules, what are packages aur I can say modules in Node JS. So pichhli video mein humne ye code likha tha, correct? We wrote just a Hello World program aur humne package.json mein start script banayi thi, correct? Toh main kya kar sakta hun? Main is program ko basically yaha par run kar sakta hun. So I can say NPM start aur isse kya hoga ye code execute kar jayega, correct? And yaha par mere paas ek warning aa rahi hai. Woh bol raha hai that please apna NPM update kar lo. Toh woh main baad mein kar lunga. So let us understand what is modules, okay? So jab hum production world mein work karte hain, you know what is modular programming, correct? Jab hum apne poore code base ko chhote-chhote modules mein divide kar dete hain usko hum kya bolte hain modular programming. For example, let us say mere paas yaha par ek function hai, okay? Let us say mere paas yaha par ek function hai jiska maine naam diya add, jo kya karta hai do numbers ko add karke return kar deta hai, correct? A plus B. Now what I can do, I can just say console.log aur main is add function ko call karunga, I'll say add, okay? And ismein let us say maine 2 and 5 ko pass kar diya. Now what will happen if I say NPM start, you are getting 7. Theek hai, due to this warning main node hello ka use karna zyada prefer karunga, so you can see 7, theek hai, taaki woh warning na aaye. So yaha par kya hua? Maine apna code yaha par rakha, maine usko execute kiya, toh ye jo meri file hai na, jo meri hello.js file hai, ye kya hai ek module hai, okay? This is a module jismein ek code hai, correct? Now, production world mein kya hoga jab tum real-world programming karoge toh tumhare paas bahut saare functions honge, bahut kuch hoga, toh tumhe kaha jayega that tumhe apne code base ko chhote-chhote modules mein divide karna hai, correct? Toh tum kya kar sakte ho? Tum ek different files bana sakte ho. You can actually split your code into different files. Toh main yaha par ek file bana sakta hun for example math.js. Toh iska mera motive kya rahega ki jitna bhi mera maths related work hai na, jitna bhi mera mathematical functions hai, woh main kya karunga is file ke andar likhunga. Theek hai, toh tum kya kar sakte ho? Tumne basically kya kiya, tumne ye function jo add tha, isko yaha se cut kiya and tumne isko yaha pe paste kar diya, okay? Ab problem kya hai? Agar main is code ko run karunga, you will get an error that add function jo hai woh defined hi nahi hai. Bahut sahi baat hai, kyuki woh toh math.js ke andar chali gayi, correct? But tum kya karna chahte ho? Tum is math.js file ko, is math.js module ko use karna chahte ho tumhari main file ke andar. Toh woh tum kaise karoge? Toh uske liye hamare paas ek function hota hai that is require function, theek hai? Toh tum kya kar sakte ho? Tum isko require kar sakte ho. Jaise Java mein hum import keyword ka use karte hain, correct? Hum C++ mein aur C mein hash include ka use karte hain. Similarly yaha par hum kya kar sakte hain? I can say const, theek hai, const. I will say math equals require, theek hai, tumhare paas built-in ek require function hota hai, require. Iske andar tum apne package ka ya fir tum apne module ka name de sakte ho jo tumhe import karna hai, jo tumhe yaha par require karna hai, theek hai? For example, agar main yaha par bolta hun, let us say buffer. Ab ye buffer kya hai? Ye kya hai na? Ye ek built-in package hai, theek hai? Ye jaise ek example ke liye. Toh ye built-in package hai, buffer ko le aayega. Lekin mujhe kya karna hai? Mujhe is math.js ko leke aana hai. Toh main kya bolunga yaha pe dot slash jiska matlab hai current directory, theek hai? Agar tum ye dot slash nahi daloge na, let us say maine yaha pe bol diya math.js ya fir sirf math toh yaha pe error aayega, okay? Agar main isko run karunga, you will see an error. Toh error kya aa raha hai? Module not found, correct? Because jab tum require function ke andar aise package ka name likhoge na toh woh kya karega? Woh na installed packages mein ya fir jo uske khud ke built-in packages hain na usmein dhoondhne lagega. Lekin hume kahan pe dhoondhna hai? Hume apni current directory ke andar dhoondhna hai, toh main isko kya bolunga dot slash, theek hai? Ab jaise hi main isko dot slash bolunga, tumhare paas error nahi aayega. So you can see that error nahi aa raha hai, lekin add function is not defined, theek hai? But you can see that ye wali line ab error throw nahi karegi. Toh tumne isko kya bola? That yaar mujhe require karke do dot slash ka matlab kya hai? Meri current working directory mein aur mujhe math naam ki file ko import karna hai. Toh usne bola, koi dikkat nahi, usne kar diya. Ab ek kaam karte hain, isko console.log karke dekhte hain ki is math ke andar hamare paas kya hai, theek hai? Toh ye hamari custom library hai, ye hamara ek tarah ka custom module hai. Toh agar main ab isko run karta hun, you will see an empty object. Toh ye add function gaya kahan? Mujhe is add function ko yaha par import karna tha, lekin ye jo math hai ye toh ek empty object aa raha hai, mujhe toh iske andar ek function chahiye tha. Toh basically hota kya hai? Jab tum ek function banate ho na, tumhe usko export bhi karna padta hai. Theek hai? Ye jo function hai na, ye abhi is module ke andar private function hai ek tarah ka. Tumhe isko export karna hai, tumhe isko na public banana hai, tumhe batana hai ki is function ko na koi bhi use kar sakta hai, theek hai? Toh tum kya ye kar sakte ho? Tumhare paas ek module object hota hai, so you can say module, theek hai? Ye ek module hai, correct? Iske exports mein abhi na kuch nahi hai. Iske exports mein na abhi kuch nahi hai. Let us say maine khud-ba-khud add kar diya, let us say maine iske exports mein daal diya khud-ba-khud Piyush, theek hai? Ek example de raha hun, theek hai? Isko samajhte hain. Flow ko maine is math.js ke andar kya kiya? Maine bola module.exports, main bol raha hun yaar is ye jo mera particular module hai na math, iske exports mein na ek string daal do Piyush. Usne kaha theek hai. Ab agar main isko run karta hun, toh is math ki value kya ho chuki hai? Piyush ho chuki hai. Toh main yaha par isko aise agar likh deta hun ki math value is and main isko console.log karta hun, abhi agar main isko run karunga, you will see math value is Piyush. Toh mujhe ek cheez samajh aa rahi hai ki yaar jo bhi main module.exports ki value kar raha hun na, woh cheez yaha se export ho rahi hai, theek hai? Toh main ye bhi toh kar sakta hun? Main yaha pe kya kar sakta hun? Main isko add function ke equal bhi kar sakta hun. Main isko kya kar sakta hun? Add function ke equal kar sakta hun. Toh ab agar main isko run karunga, toh math ki jo value aa rahi hai, woh ek function aa rahi hai. Woh kya aa rahi hai? Ek function aa rahi hai jo ek add function hai, bahut badhiya. Toh matlab main is math function ko directly call karke do number pass kar sakta hun that is 2 and let us say 4 and agar main isko run karta hun, toh mere paas value kya aa rahi hai? 6. Matlab jo mera add function hai, jo mera add wala function tha, woh mera hello.js ke andar aa gaya, why? Because maine module.exports ko kiske equal kar diya? Add function ke equal kar diya. Yaha tak toh theek hai. Ab let us say because tumhara ye math.js aur bhi arithmetic operators tumhare paas ho sakte hain, aur bhi tumhare paas operations ho sakte hain. Toh tumne let us say ek aur function banaya subtract, okay, A and B and ye kya karega? Ye do numbers ko na subtract kar dega, so ye A minus B kar dega. Ab tujhe, ab hume ye subtract function bhi yaha par hume chahiye, correct? So most probably, most probably tumhare dimaag mein kya approach aayegi? So I think jo matlab pehli baar maine socha tha toh maine kuch aisa kiya. Toh maine isko aisa kar diya tha, theek hai, ye galat hai, this is wrong. Ab agar main isko run karta hun, so you are getting minus 2. Lekin ab add function kaise kare? Theek hai? Ab agar main tumhe waise math dikhata hun, agar main tumhe maths ki value dikhata hun na, so now this is a subtract function, theek hai? Ab problem kya ho raha hai isse? Woh na overwrite kar raha hai value ko, woh kya kar raha hai? Woh exports mein pehle maine add dala fir subtract daal diya, toh ye add function jo hai na, woh overwrite ho gaya. Woh kya ho gaya? Overwrite ho gaya. Let us say agar mere paas yaha par hota 1 and yaha par mere paas hota 2, toh mere paas ultimate value kya aa rahi hai? 2. Matlab mera jo 1 hai, woh mera kya ho raha hai? Lost ho raha hai. So what you can do is, tum JavaScript objects ka use kar sakte ho to return multiple things, theek hai? Toh main kya kar sakta hun? module.exports ek object, theek hai? Ek object jiske andar ek toh maine add function daal diya aur ek maine kya daal diya? Subtract function daal diya. Ab agar main isko run karta hun, you can see that hamare paas ek object hai, theek hai? Hamare paas hello.js ke andar ek object hai. Jiske andar you can see mere paas ek add function hai and ek mere paas kya hai? Subtract function hai. Main inko rename bhi kar sakta hun, main inko ye bhi bol sakta hun ki add, add function ko maine iske equal kar diya aur maine bol diya subtract function, FN function aur maine isko iske equal kar diya. Ab agar main isko run karunga, toh mere paas kya aa raha hai? Add function ek add function hai and subtract function ek subtract function hai. Ab tum isko kaise use kar sakte ho? You can say math, theek hai? Tumne import kar liya dot. Ab yaha pe tumhare paas do functions aa rahe hain, correct? Ek hai addFn aur ek hai subtract function. Toh let us say maine add function ko call kiya 2 and 4 ko pass kiya and agar main isko run karunga, you are getting 6, which is perfectly fine. Similarly, agar main yaha pe subtract function ko call karta hun toh hamare paas kya aayega? Toh hamare paas negative 2 aa raha hai, A minus B ho raha hai, perfect, ye bhi bilkul sahi kaam kar raha hai, correct? And agar yaha pe maine sirf aur add ko is tarah se hi rakh diya, theek hai, toh woh add and subtract ko maine yaha se export kar diya, so now ye kya aa jayega? Ek add function, ek subtract function hai. Toh main isko subtract ko bhi call kar sakta hun, add ko bhi call kar sakta hun, tum usko kuch bhi name de sakte ho, correct? Even you can destructure it, theek hai? Toh main yaha pe destructuring bhi kar sakta hun. So I can say that add function le liya, maine yaha pe subtract function le liya, correct? Toh ab main kya kar sakta hun? Main directly add function ko use kar sakta hun, I can directly use add function and I can directly use this subtract function and mera add function bhi work kar raha hai, subtract function bhi work kar raha hai. So humne kya kiya? Humne ek file banayi, humne ek different file banayi jiske andar humne kuch functions rakhe and in functions ko in, jo bhi tum yaha par kaam karoge, chahe tum ek function bana rahe ho, chahe tum kuch object bana rahe ho. Tumhe usko export karna padega that tabhi woh baaki modules mein available hoga, so you have to export that thing, number one thing. Uske baad hum usko import kaise kar sakte hain? You have you can use the require function jo built-in hota hai Node JS ke andar, theek hai? Ye JavaScript mein nahi hota, ye sirf aur sirf Node JS ke andar hota hai, so you have this require function. And is require function ke andar tum kya kar sakte ho? Tum uska path de sakte ho that kaun se package ko import karna hai. And then you can basically make make that equal to a variable, matlab jo bhi uska import aaya, jo bhi wahan se hamare paas functions aaye, usko maine ek object mein ya fir ek maine ek variable mein store kar liya. And ab main kya kar sakta hun? Is variable ko use karke, I can use this variable to access that functions. Toh yaha tak toh hume baat clear ho gayi, theek hai? Ab export karne ka, isko hum bolte hain multiple export, theek hai? Is cheez ko hum kya bolte hain? Ye multi, this is a single export, this is a default export ek tarah ka, theek hai? Ek aur tarika hai export karne ka, that is exports function ko use, exports object ko use karke. Toh main kya kar sakta hun? Main isko hata deta hun, theek hai? Main isko comment out kar sakta hun. So what I can say? I can say exports, theek hai? Dot main iske upar ek value laga sakta hun khud ki add. Toh ye ek arrow function hai, let us say A and B jo return kya karta hai A plus B. Similarly, I can say exports dot. Uh, then I can say subtract, theek hai, subtract equals A and B tum mujhe do parameters doge and I will return you A minus B. Toh ye bhi kya hai? Ye bhi ek tarah ka exports hai, theek hai? Ye hum multiple exports kar rahe hain. Toh agar ab main wapas aata hun and I console.log my math, you can see mere paas exactly same value aa rahi hai, mere paas exactly same value, lekin this is an anonymous function, theek hai? Toh ye kya hai? Ek anonymous function hai because technically na is function ka koi name nahi hai. Ye jo function hai na iska koi name hi nahi hai, correct? Kyuki ye add toh maine bola, ye ek property hai, this is a property, theek hai? Main isko add one bhi bol sakta tha, main isko subtract 2 bhi bol sakta tha. Ab agar main isko run karta hun, you are getting add one and subtract 2 jo kya hai? Ek function hai which is an anonymous function. Hume nahi pata ye function kya karta hai, hume nahi pata in functions ka name kya hai. This is an anonymous functions. Theek hai? Because hum yaha par functions ka ek name de rahe the, that's why we were getting the name of the function, lekin yaha par ye kya hai? Ek anonymous functions hai. So hamare paas do tarike hain. Either you can do this way or you can say module.exports. Matlab tum exports ka directly use bhi kar sakte ho, uske upar value append kar sakte ho jo bhi tumhe export karna hai. And you can even do like tum apne jitna bhi tum apna code hai, tum usko upar code kar sakte ho and then you can basically export that as an object, okay? Module.exports ek hi baar use kar sakte ho because woh value ko overwrite kar deta hai, exports dot karke tum jitni marzi values ko export kar sakte ho. Ab kaun sa preferred hai, kaun sa nahi? Dekho, ye use case pe depend karta hai, right? Agar tumhare paas ek aisa code hai jiske andar bahut saare functions hain aur woh chalte hi ja rahe hain, then maybe it's a good practice to use exports, theek hai? But I personally prefer this way, theek hai? Toh main kya karta hun? Main apne jitne bhi functions hain woh upar banata rehta hun and module.exports ke andar finally main unko export kar deta hun, okay? So this was a way to export your functions and require function ko use karke tum unko import kar sakte ho. Ye baat toh hume samajh aa gayi. Similarly, what you can do is, jaise maine tumhe bataya tha ki Node JS ke andar na hamare paas bahut saare built-in packages, built-in modules hote hain, okay? Toh tum unka bhi use kar sakte ho. Toh agar, jaise agar main yaha par control space ya fir agar tumhare paas Macbook hai, command space dabate ho, toh ye jitne bhi packages ka naam aa raha hai na, you can see ye saare kya hain? Ye hamare na built-in packages hain. Ye kya hai? Hamare built-in packages hain, for example, hamare paas ek package hota hai HTTP, okay? Toh is is package ko use karke hum kya kar sakte hain? Hum apne web servers bana sakte hain jo hum agli video mein banane wale hain, not agli video, matlab ek upcoming video mein hum banane wale hain. Similarly, hamare paas ek FS module hota hai, ek FS, uh, hota hai jisko hum bolte hain file system jisko hum next video mein dekhenge. Toh isko use karke main kya kar sakta hun? File handling kar sakta hun. So require function mein kya hota hai? Agar tum kisi package ka name as it is de dete ho toh woh apne node ki directory mein dhoondhta hai, theek hai? Matlab kahan par uske paas jahan par saare packages padhe hain. Agar tum yaha par dot slash de dete ho toh woh kahan dhoondhega? Current directory ke andar, okay, current directory. Ab agar main isko run karunga, you will get an error because current directory mein tumhare paas FS naam ka koi module nahi hai. Lekin agar main isko sirf FS karke naam deke isko main run karta hun, you can see that hamare paas bahut saare functions, bahut saari cheeze aa rahi hain which I can use. Okay? So, this was the require function and this was a brief introduction on modules ki modules kya hote hain, hum modules ko kaise use kar sakte hain, require function kya hota hai, and jo hum khud ke functions ya fir hum khud ke kuch properties banate hain, usko hum kaise export kar sakte hain. So, I hope tumhe ye clarity mili hogi that what are modules, how modular programming is done in Node JS, theek hai? Java mein hum kya karte hain? Hum apne packages banate hain, correct? If you remember, hum ek package banate hain fir hum bolte the import jaise java.util.star. Similarly, agar tumne kabhi C aur C++ ke saath kaam kiya hai, you remember hum hash include ka use karte the. Toh similarly, yaha par hum kya use karte hain? Hum require function ka use karte hain jo bhi hume import karna hai, okay? Dot matlab agar hum dot se start karenge toh woh current directory mein dhoondhega hamare project directory mein, agar hum directly uska name de denge toh woh kahan dhoondhega? Apne built-in packages mein. Ya fir agar humne koi external package install kiya hai wahan pe, abhi tak humne koi external packages install nahi kiye hain. So jo ye FS hai, jo HTTP hai, ye kya hote hain? Ye built-in aate hain. Toh isliye that's why hume inko install karne ki zaroorat nahi pad rahi. Similarly, tumhare paas ek aur hota hai, ek aur package hota hai that is crypto, okay? Toh ye cryptography mein use hota hai, cryptography in the sense agar tumne kahin pe hashing karni hai, tumhe salts generate karne hain, toh ye wahan par use hota hai like encryption, decryption, toh wahan par crypto package use hota hai. Toh Node JS ke andar bahut saare built-in modules hain jo bahut kaam aate hain and bahut powerful hain. So this was a quick video on modules. I hope tumhe modules ache se samajh aa gaya hoga. Baaki ab jitne bhi hum aage programs karenge, hum sab mein ye modules use karne wale hain. Hum apne program ko poora modular rakhne wale hain, hum bahut saare packages ko require karenge, hum third-party packages ko install bhi karenge toh ye cheez aur clear hoti jayegi. But I hope that tumhe ye wali cheez clear ho gayi hogi that module.exports kya hota hai, how to export your function. So that's all for this video. Agar tumhe kuch bhi doubt ho, please feel free to post that in comments, main comments ko regularly read karta hun. And video acha laga ho toh like and subscribe zaroor karna. Milte hain aapko next video mein, until then, bye-bye, take care.
Watch on YouTube
Share
MORE TRANSCRIPTS



