6

I have been writing modules in nodejs as following :

module.exports = function (logger, db, external,constants) {

        return {
           //something

        }
    }

Recently someone in my team suggested that whole script should be wrapped in a function to avoid global confusion of variables i.e. like this :

(function () {
    'use strict';
    module.exports = function (logger, db, external,constants) {

        return {
               //something
        }
    }

}());

I understand that this practice is generally used at client side code. But during server side in nodejs is this required? I think that in nodejs there is really no global scope and only module.exports is the one which is accessible really irrespective of whatever we write in script file (ofcourse don't go wild over here).

Sikorski
  • 2,653
  • 3
  • 25
  • 46

2 Answers2

12

No, IIFEs aren't required with Node.js.

They can be useful for any scripts that may be used in multiple environments (UMD).

But, each module/file executed by Node.js is given a "module scope," similar to the scope an IIFE provides, as described under "Globals":

In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

Though, there is still a global scope with Node.js. When a module creates a global, it will be accessible in other modules used by the same process.

foo = 'bar'; // lack of `var` defines a global

console.log(global.foo); // 'bar'
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
1

You're actually already doing this.

What he's suggesting is to wrap the entire script in a function like this:

function () {

}

That's all. Nothing special. Of course, in regular javascript a function definition just defines a function and the code inside the function doesn't run. So to automatically run the function you wrap it in an expression context and invoke it:

(function () {

})()

However, in node.js you don't need to do this. Instead you can simply call the function when you require the module. So in node.js, this does the exact same thing in terms of creating a private scope:

module.exports = function () {

}

So tell your friend you're already wrapping the entire script in a function.

This is perhaps the first case that I see the harm in naming things and design patterns. In this case your friend is thinking IIFE. But IIFE is nothing special. IIFE doesn't create a private scope. It's functions that create scope. IIFE is just a means for the function to call itself. Which is why I prefer calling it self-calling-function to avoid giving it a sense of magic which may cause some people to perceive it as something special.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • That was my first thought when I saw his examples: he's already wrapping his module in a function. – qxn Jul 23 '15 at 14:01
  • is there any way that some third-party js like Google one tap in our js and provide as Script to be embedded in some HTML Thanks – Sudhir Kaushik Apr 29 '23 at 18:28
  • @SudhirKaushik Not within javascript itself. That's the whole point of scopes - in any language: to block code outside the scope from accessing things inside the scope. Of course, since js is just text you can always download the js file and copy/paste the code into your own HTML. But that's access to the **code** which is different from access to your **variables** and **functions**. – slebetman Apr 30 '23 at 00:10
  • So basically its IIFE. But how does a script like Google analytics script goes into my IIFE to get executed, that too having some Client ID passed? Have I made something? not yet, but I want to understand the concept through SO first. – Sudhir Kaushik Apr 30 '23 at 17:05
  • If you read to the bottom of my answer I actually said it's just IIFE and IIFE is nothing special - it's just a function. GA cannot go into your function. Instead you call GA functions from inside your function. As for how GA gets data from your code, you passed it to GA when you call the GA functions. What GA does "automagically" is parse your DOM for some relevant metadata in your HTML and intercept user events like mouse click. – slebetman May 01 '23 at 02:46