Skip to content

Instantly share code, notes, and snippets.

@madox2
Last active August 10, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madox2/e9e21ef9db801028d2be1f11bfa476a3 to your computer and use it in GitHub Desktop.
Save madox2/e9e21ef9db801028d2be1f11bfa476a3 to your computer and use it in GitHub Desktop.
intercepting module in node
const M = require('module')
const _load = M._load;
M._load = function(request, ...rest) {
const _module = _load(request, ...rest)
if (request === './module1.js') {
return function(...args) {
console.log(`intercepting ${request}`)
_module(...args)
}
}
return _module;
}
require('./module1.js')()
require('./module2.js')()
module.exports = () => console.log('hello from module1')
module.exports = () => console.log('hello from module2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment