M E R N S TA C K — N O D E .
J S F O U N D AT I O N
Module 2: The Module System
Code ko chhote-chhote files mein todna — require/exports
aur import/export, dono tariko se.
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327
ROADMAP
Is Module Mein Kya Kya Cover Hoga
1 2
Code ko modules mein kyun todte hain CommonJS — require() aur [Link]
3 4
[Link] vs exports ka gotcha ES Modules — import/export, Node mein enable karna
5 6
Built-in modules — path, os, events Real-world: chhoti app ko modules mein organize karna
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 2
QUICK RECAP
Module 1 Se Yaad Hai?
• [Link] install kiya, npm samjha, apna pehla project bana ke nodemon se chalaya.
• Ab tak humne sirf ek hi file ([Link]) mein sab kuch likha — real apps mein ye kaam nahi karta.
• React mein tumne import/export use kiya tha bina soche ki ye kaam kaise karta hai.
• Aaj samjhenge: Node mein modules kaise kaam karte hain — do tariko se: purana (CommonJS) aur naya (ES Modules).
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 3
THE PROBLEM
Ek Hi File Mein Sab Kuch — Kyun Bura Idea Hai
Socho ek real app mein 2000 lines ka ek hi [Link] — sab kuch ek jagah: routes, database logic, helper functions, sab mixed.
Dhoondna Mushkil
Ek chhoti si cheez find karne ke liye bhi poori file scroll karni padegi
Reuse Nahi Ho Sakta
Same helper function 3 jagah chahiye? Copy-paste karna padega
Team Kaam Nahi Kar Sakti
Do log ek hi file edit karein to Git conflicts ka dher lag jayega
Solution: code ko chhote, focused files mein todo — aur ek dusre se 'import' karo jab zaroorat ho.
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 4
COMMONJS
require() Aur [Link]
Node ka original (purana, par abhi bhi bahut common) module system — CommonJS.
[Link] [Link]
function add(a, b) { const add = require('./math')
return a + b
} [Link](add(2, 3)) // 5
[Link] = add
[Link] = jo bhi is file se 'bahar' bhejna hai. require('./math') = '[Link]' ko import karo — .js extension aur './' zaroori nahi likhna, Node khud
dhoond leta hai.
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 5
COMMONJS
Multiple Cheezein Export Karna
Ek se zyada functions/values export karne ke liye — ek object export karo.
[Link] [Link]
function add(a, b) { return a + b } const { add, subtract } = require('./math')
function subtract(a, b) { return a - b }
[Link](add(5, 2)) // 7
[Link] = { [Link](subtract(5, 2)) // 3
add,
subtract // destructuring — bilkul React props jaisa!
}
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 6
THE GOTCHA
[Link] vs exports
'exports' asal mein '[Link]' ka hi ek shortcut/reference hai — par ek trap hai jisse bachna hai.
✔ Ye theek chalega ❌ Ye NAHI chalega
[Link] = add exports = { add, subtract }
[Link] = subtract
// exports ko poora REPLACE kar diya —
// properties ADD kar rahe ho // ab ye [Link] se disconnect ho
gaya!
Safest Rule: hamesha [Link] use karo (poora naam), khaaskar jab poora object ek saath export karna ho. Ye confusion se bachata hai.
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 7
BEHIND THE SCENES
require() Ek Baar Hi Chalta Hai
Node smart hai — same file ko baar-baar require karo, wo dobara 'run' nahi hoti, sirf cached result milta hai.
// [Link]
[Link]("[Link] loaded!")
[Link] = "I am the logger"
// [Link]
require('./logger')
require('./logger')
require('./logger')
// Console mein "[Link] loaded!" SIRF EK BAAR chhapega
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 8
THE MODERN WAY
ES Modules — import/export
Wahi syntax jo tumne React mein use kiya — Node mein bhi chal sakta hai, bas ek chhota switch on karna padta hai.
[Link] mein enable karo: Ab yehi syntax chalega:
{ // [Link]
"type": "module" export function add(a, b) {
} return a + b
}
// [Link]
import { add } from './[Link]'
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 9
COMPARE KARO
CommonJS vs ES Modules
CommonJS ES Modules
Import require('./math') import ... from './[Link]'
Export [Link] = ... export function / export default
Extension in path Optional (.js na likho bhi chalta) Zaroori — './[Link]'
Enable karna Default hai Node mein "type": "module" chahiye
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 10
BUILT-IN MODULE
path — File Paths Ke Saath Kaam Karna
Windows aur Mac/Linux mein file paths alag likhe jaate hain — path module ye farak khud sambhal leta hai.
const path = require('path')
[Link]('folder', '[Link]') // 'folder/[Link]'
[Link]('/a/b/[Link]') // '[Link]'
[Link]('[Link]') // '.txt'
[Link]('/a/b/[Link]') // '/a/b'
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 11
BUILT-IN MODULE
os — Computer Ke Baare Mein Jaankari
Kam use hota hai, par kabhi kabhi system-level info chahiye hoti hai.
const os = require('os')
[Link]() // 'win32', 'darwin', 'linux'
[Link]().length // kitne CPU cores hain
[Link]() // free memory (bytes mein)
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 12
BUILT-IN MODULE
events — EventEmitter Ka Introduction
React mein onClick jaisa hi socho — 'kuch hua', to sunne wale ko batao. Node ke andar-andar bahut kuch isi pattern se chalta hai.
const { EventEmitter } = require('events')
const emitter = new EventEmitter()
// 'sun rahe ho' — jab bhi 'studentAdded' event ho
[Link]('studentAdded', (name) => {
[Link](`Naya student: ${name}`)
})
// 'event fire karo' — jab bhi naya student add ho
[Link]('studentAdded', 'Ananya')
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 13
PUT IT TOGETHER
Real Example — Chhoti App, Organized
Ek Student Manager script — logic aur data alag files mein.
// [Link]
const students = [{ name: "Ananya", score: 92 }]
[Link] = students
// [Link]
function getTopScorer(students) {
return [Link]((a, b) => [Link] - [Link])[0]
}
[Link] = { getTopScorer }
// [Link]
const students = require('./students')
const { getTopScorer } = require('./helpers')
[Link](getTopScorer(students))
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 14
WATCH OUT
Common Module Mistakes
✕
exports = {...} likhna (poora replace)
Fix: [Link] = {...} use karo, ya [Link] = value karo
✕
require path mein extension bhool jaana (built-ins ke liye)
Fix: apni files ke liye './file' theek hai, built-in modules ka naam seedha ('path', 'os')
✕
CommonJS aur ES Modules mix karna bina type set kiye
Fix: ek project mein ek hi style rakho — "type": "module" set karke decide karo
✕
Circular requires (A, B ko require kare, B, A ko)
Fix: shared logic ko teesri file mein nikaal ke dono se use karo
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 15
WRAP UP
Quick Recap + Practice Task
• CommonJS: require() se import, [Link] se export
• exports use karte waqt sirf properties add karo, poora replace mat karo 🎯 PRACTICE TASK
• require() cached hota hai — ek baar hi chalta hai 1. Ek [Link] banao jo ek function export kare, [Link] se
• ES Modules: "type": "module" set karke import/export use karo use karo
• Built-in modules: path, os, events — bina install kiye milte hain
2. Usi file mein 2-3 aur functions add karo, object ki tarah
export karo
3. Ek naya project banao, "type": "module" set karke
import/export try karo
4. Bonus: path module se apne project ki koi file ka extension
nikaalo
Next Module → File System (fs)
Ycotes Computer Classes | Prepared by Chirag Sir | +91 9216988327 16
Shukriya!
Questions? Doubts? Comment ya class mein poocho.
Agle module mein milte hain — File System (fs) ke saath.
YC OT E S C O M P U T E R C L A S S E S