Every module injected only once

suggest change

Node.js runs a CommonJS module only the first time you require it. Later require calls return the same exports object, so the module body does not run again. That cache avoids repeated file reads. ESM import has its own module cache and is not the same call.

myModule.js

console.log(123) ; 
exports.var1 = 4 ;
var a=require('./myModule') ; // Output 123
var b=require('./myModule') ; // No output
console.log(a.var1) ; // Output 4
console.log(b.var1) ; // Output 4
a.var2 = 5 ; 
console.log(b.var2) ; // Output 5

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents