How does require in node.js deal with globals?

I just found out that if I require a module and store it as a global, I can overwrite methods and properties in the module as shown below:

global.passwordhelper_mock = require("helpers/password")
sinon.stub(passwordhelper_mock, "checkPassword").returns true

If I then require another module which in itself utilizes the above stubbed method, my stubbed version will be used.

How does the require function in node.js take notice to these globals? Why does it only work when I overwrite/stub a module that has been saved as a global?

Thanks

How does the require function in node.js take notice to these globals?

Somewhere inside the module there must be a call to module.exports.someObject = function(x) {...} in order for someObject to be come available globally.

Why does it only work when I overwrite/stub a module that has been saved as a global?

Not sure I follow here. If the object was hidden then you couldn't overwrite it. You can overwrite any object available to you, either a global object (e.g. console) or a property of any object available to you at runtime (e.g. console.log).