Does making a IIFE make sense in a Node.js module?


Working in JavaScript in a browser I always use a IIFE to save global name space pollution.

(function() {})();

but as Node.js uses common.js and almost every file is a module in itself, does it make sense to make IIFEs (if using ES5) when starting new code?


No, it doesn’t.

Internally, Node already wraps the contents of the module with a function to create a new scope.

You can find the specifics in this file; the function by which the module contents are wrapped is defined here, and in this method you see that after wrapping the module code, it’s executed almost immediately (similar to what an IIFE does).

Answered By – robertklep

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.