Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monkey patch node module

I'm trying to monkey patch this node module in electron application. I want to change the capture method input arguments.

My code so far looks like this:

if (process.platform == "darwin") {
      let refSCapt = screenshot.capture;
      console.log("Outside");
      screenshot.capture = function(output: string, callback: any) {
        console.log("Inside");
        // Override output path of a temp .png file
        let tempOutput = output.split("/")[-1];
        refSCapt(this.screenshotsPath + tempOutput, callback);
      };
    }

The problem is that the patch is not reflecting and the original method is called as if nothing would've change. The Outside get's logged but the Inside is never called.

So how can I monkey patch this module method?

like image 446
intelis Avatar asked Oct 27 '25 13:10

intelis


1 Answers

So how can I monkey patch this module method?

What you have would work fine ... but only if your code runs before it is used elsewhere. For reliable patching I recommend https://github.com/ds300/patch-package which patches modules on install.

like image 136
basarat Avatar answered Oct 29 '25 04:10

basarat