Monkeypatching the require function in node.js may prove useful, especially for some libraries. I am trying to figure out how I could do it right, safely, etc.
Here is what I have:
const Mod = require('module');
const req = Mod.prototype.require;
Mod.prototype.require = function () {
// do some side-effect of your own
req.apply(this, arguments);
};
however this isn't quite working and I am not sure why. I get this error from with the debug module:
TypeError: Cannot set property 'init' of undefined
at Object.<anonymous> (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/debug/src/node.js:15:14)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at Module.Mod.require (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/lib/index.js:11:9)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/debug/src/index.js:9:20)
if my code is OK, then maybe I should take a closer look at what the debug module is doing?
via Alexander Mills
No comments:
Post a Comment