猴子补丁节点模块

int*_*lis 1 node.js typescript electron

我正在尝试在电子应用中猴子修补该节点模块。我想更改方法输入参数。capture

到目前为止,我的代码如下所示:

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);
      };
    }
Run Code Online (Sandbox Code Playgroud)

问题在于补丁没有反映出来,原始方法的调用好像什么都不会改变。在Outside获取真实记录,但Inside永远不会被调用。

那么我该如何猴子修补此模块方法呢?

bas*_*rat 6

那么我该如何猴子修补此模块方法呢?

您所拥有的一切都可以正常工作……但前提是您的代码在其他地方使用之前运行。为了进行可靠的修补,我建议使用https://github.com/ds300/patch-package修补安装的模块。