Override the require function

后端 未结 4 1156
故里飘歌
故里飘歌 2020-12-08 10:45

Is it possible to override the global require function, affecting it at process level?

From what I know, the require

相关标签:
4条回答
  • 2020-12-08 10:54

    mock-require does this by overriding Module._load (which is what the real require actually calls).

    0 讨论(0)
  • 2020-12-08 11:08

    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.

    var Module = require('module');
    Module.prototype.require = new Proxy(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"
            }
    
            return Reflect.apply(target, thisArg, argumentsList)
        }
    })
    
    0 讨论(0)
  • 2020-12-08 11:08

    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];
        function newRequire () {
            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.

    0 讨论(0)
  • 2020-12-08 11:13
    var Module = require('module');
    var originalRequire = Module.prototype.require;
    
    Module.prototype.require = function(){
      //do your thing here
      return originalRequire.apply(this, arguments);
    };
    
    0 讨论(0)
提交回复
热议问题