Skip to content

Instantly share code, notes, and snippets.

@mgttt
Forked from adam-lynch/requireNoCache.js
Last active March 25, 2017 02:57
Show Gist options
  • Save mgttt/aaf27e631154abc95c265ecc28289e54 to your computer and use it in GitHub Desktop.
Save mgttt/aaf27e631154abc95c265ecc28289e54 to your computer and use it in GitHub Desktop.
Require a module, without going to Node.js's require cache
var _invalidateRequireCacheForFile = function(mmm){
try{
var p=require.resolve(mmm);
delete require.cache[p];
}catch(ex){};
};
var try_require=function(mmm,nocache){
if(nocache) _invalidateRequireCacheForFile(mmm);
try{
return require(mmm);
}catch(ex){
//TODO 判断如果是网络的就抓回到 tmp再require
//如果是本地的,尝试到 ./src/ ../上面再找找
return null;
}
};
module.exports=try_require;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment