0

When writing JavaScript for webpages it is good practice to wrap files in an anonymous self-executing function to avoid polluting the global namespace. See What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

When writing JavaScript files in a server-side NodeJS environment is it still useful to wrap my files in this anonymous function?

Community
  • 1
  • 1
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • 1
    One of the benefits of node modules is that nothing is global by default as a module is already in it's own private namespace. So only things you explicitly attach to the `global` object will be global. – jfriend00 Sep 10 '15 at 05:57

1 Answers1

3

No you don't need to do that in Node, your code is already wrapped in a function by default:

(function (exports, require, module, __filename, __dirname) {
  // YOUR CODE INJECTED HERE!
});

For more information, read http://fredkschott.com/post/2014/06/require-and-the-module-system/.

jgillich
  • 71,459
  • 6
  • 57
  • 85