Singleton pattern in nodejs - is it needed?

I recently came across this article on how to write a singleton in nodejs

http://simplapi.wordpress.com/2012/05/14/node-js-singleton-structure/

I know that the documentation of require states that:

Modules are cached after the first time they are loaded. 
Multiple calls to require('foo') may not cause the module code to be executed multiple times.

So it seems that every required module can be easily used a singleton without the singleton boilerplate code.

Question:

Does the above article provide a round about solution to creating a singleton?

All of the above is overcomplicated. There is a school of thought which says design patterns are showing deficiencies of actual language.

Languages with prototype-based OOP (classless) do not need a singleton pattern at all. You simply create a single(ton) object on the fly and then use it.

As for modules in node, yes, by default they are cached, but it can be tweaked for example if you want hot-loading of module changes.

But yes, if you want to use shared object all over, putting it in a module exports is fine. Just do not complicate it with "singleton pattern", no need for it in JavaScript.

A singleton in node.js (or in browser JS, for that matter) like that is completely unnecessary.

Since modules are cached and stateful, the example given on the link you provided could easily be rewritten much more simply:

var socketList = {};

exports.add = function (userId, socket) {
    if (!socketList[userId]) {
        socketList[userId] = socket;
    }
};

exports.remove = function (userId) {
    delete socketList[userId];
};

exports.getSocketList = function () {
    return socketList;
};
// or
// exports.socketList = socketList

Looking a little further at the Module Caching Caveats in the Modules docs:

Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), **it is not a guarantee** that require('foo') will always return the exact same object, if it would resolve to different files.

So, depending on where you are when you're requiring a module, it's possible to get a different instance of the module.

Sounds like Modules are not a simple solution to creating singletons.

Edit: Or maybe they are. Like @mkoryak, I can't come up with a case where a single file might resolve to different filenames (without using symlinks...). But (as @JohnnyHK comments), multiple copies of a file in different node_modules directories will each be loaded and be

You don't need anything special to do a singleton in js, the code in the article could just as well be:

var socketList = {};

module.exports = {
      add: function() {

      },

      ...
};

Outside node.js (for instance, in browser js), you need to add the wrapper function manually (it is done automatically in node.js):

var singleton = function() {
    var socketList = {};
    return {
        add: function() {},
        ...
    };
}();

You can use my create-singleton module (https://github.com/tarunc/createSingleton)

var createSingleton = require('create-singleton');

var mySingleton = createSingleton(function mySingleton() {
  // Describes my singleton class
  var myPrivateVariable = 5;

  this.myPublicFunction = function() {
    // something cool happens
  };
});

// Contrust the singleton class
var myInstance1 = new mySingleton();

// Later on

var myInstance2 = new mySingleton();
// This doesn't create a new instance of mySingleton but instead returns the same one
// myInstance1 === myInstance2