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
Is there some reason for this specific way? Why just don't export like usual?
3 Replies
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.
How it was done before?
CommonJS is
module.exports = { nameToExpose }
& ESM is export const nameToExpose =
.