You don't actually need a new object (exports already is one), but you do need to make sure within your module you reference exports's attribute that points to the function, rather than referencing the function by name, e.g.
function doSomething() { ... }
exports.doSomething = doSomething;
exports.doSomethingElse = function () {
// doSomething(); // this will always call the local function
exports.doSomething(); // calls the exported function, which can be overwritten
}
In practice if doSomething is a function that generally you'd expect to be private to the module and only need to override under special
circumstances I'd recommend either giving it a weird prefix (e.g. _doSomething) or embedding it in a sub-object, e.g.
var private = {
doSomething: function () { ... }
};
exports.private = private; // export private methods for testing
exports.doSomethingElse = function () { private.doSomething(); }
Then when you're overriding it it will be more obvious that this is an odd thing to do
var myMod = require("myMod);
myMod.private.doSomething = function () { ... }; // override private doSomething method
If you wanted to get fancy you could even make the private export conditional on environment e.g.
if (process.env.NODE_ENV === "development") {
exports.private = private; // export private methods for testing