Skip to content Skip to sidebar Skip to footer

Override The Require Function

Is it possible to override the global require function, affecting it at process level? From what I know, the require function is provided as argument in the function that wraps the

Solution 1:

varModule = require('module');
var originalRequire = Module.prototype.require;

Module.prototype.require = function(){
  //do your thing herereturn originalRequire.apply(this, arguments);
};

Solution 2:

Solution 3:

Here is a much safer native ES6 answer based on @orourkedd which acts like an event listener on all require calls, It might look like its replacing require but if you look closer its actually saying: require = require and trap calls to it but return original behaviour. This is just one of the millions of uses of Proxy() which has been really handy for me, for example in Typescript for mapping tsconfig "paths" with the real module node will resolve.

I would go as far as to say that this is not "Monkey patching" as its on a lower level of the language.

varModule = require('module');
Module.prototype.require = newProxy(Module.prototype.require,{
    apply(target, thisArg, argumentsList){

        let name = argumentsList[0];

        /*do stuff to ANY module here*/if(/MyModule/g.test(name)){
            /*do stuff to MY module*/
            name = "resolveAnotherName"
        }

        returnReflect.apply(target, thisArg, argumentsList)
    }
})

Solution 4:

This is the workaround I found. If there is any better solution, I'm open to see it.

I created a script named req-modifier.js:

module.exports = function (_args) {

    var _require = _args[1];
    functionnewRequire () {
        console.log("Require is called");
        return _require.apply(this, arguments);
    }          
    newRequire.__proto__ = _require;

    _args[1] = newRequire;
};  

Then from the files I want to modify the require function, I do:

require("./req-modifier")(arguments);

var foo = require("./foo");

The limitation is that I have to call every time the req-modifier function.

Post a Comment for "Override The Require Function"