Why do people use IIFE to export nodejs modules?

When watching or reading tutorial content about nodejs, I often see people exporting modules like the following
module.exports = (function(){
return {
nameToExpose: functionToExpose
...
};
})();
module.exports = (function(){
return {
nameToExpose: functionToExpose
...
};
})();
Is there some reason for this specific way? Why just don't export like usual?
3 Replies
dys 🐙
dys 🐙11mo ago
This is the CommonJS format for exporting which is how things were done before ECMAScript Modules became the standard around 2016. Some projects still use it for compatibility reasons.
Pi, a future fluent jp speaker
How it was done before?
dys 🐙
dys 🐙11mo ago
CommonJS is module.exports = { nameToExpose } & ESM is export const nameToExpose =.
Want results from more Discord servers?
Add your server