Skip to content

Instantly share code, notes, and snippets.

@zzuhan
Last active March 15, 2019 06:55
Show Gist options
  • Save zzuhan/4bd459bfc657a256bcaf04509678df2b to your computer and use it in GitHub Desktop.
Save zzuhan/4bd459bfc657a256bcaf04509678df2b to your computer and use it in GitHub Desktop.
[node-overwrite] nodejs中覆盖 #nodejs
// 方法1
var Module = require('module');
const originalLoad = Module._load;
// 重写require方法,用来覆盖vscode
Module._load = function(request, parent){
if(request == 'vscode') {
return require('./mock/vscode');
}
return originalLoad.apply(this, [].slice.call(arguments));
}
// 方法二
var Module = require('module');
var originalRequire = Module.prototype.require;
Module.prototype.require = function(){
//do your thing here
return originalRequire.apply(this, arguments);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment