2017-03-19 2 views
1

node.jsのrequire関数をMonkeypatchすると、特に一部のライブラリで便利です。Node.jsのrequire関数を安全かつ適切にmonkeypatchする

const Mod = require('module'); 

const req = Mod.prototype.require; 

Mod.prototype.require = function() { 
    // do some side-effect of your own 
    req.apply(this, arguments); 
}; 

しかし、これはかなりの作業ではないと私はなぜわからない:私は

ここ

は私が持っているものであるなど、安全に、私は右のそれを行う可能性がどのように把握しようとしています。私は、デバッグ・モジュールとから、このエラーを取得する:

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) 

私のコードがOKであれば、多分私はデバッグモジュールが何をしているかを詳しく見てみなければなりませんか?あなたのラッパーが常にundefined返すreturnなければ

Mod.prototype.require = function() { 
    // do some side-effect of your own 
    return req.apply(this, arguments); 
}; 

答えて

2

あなたは結果を返しませんでした。

+0

うん、それは、ありがとう! –

関連する問題