Skip to content

Instantly share code, notes, and snippets.

@adam-lynch
Last active January 4, 2022 01:47
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save adam-lynch/11037907 to your computer and use it in GitHub Desktop.
Save adam-lynch/11037907 to your computer and use it in GitHub Desktop.
Require a module, without going to Node.js's require cache
var path = require('path');
var _invalidateRequireCacheForFile = function(filePath){
delete require.cache[path.resolve(filePath)];
};
var requireNoCache = function(filePath){
_invalidateRequireCacheForFile(filePath);
return require(filePath);
};
@mibac138
Copy link

Thank you! :)
👍

@facekapow
Copy link

I might suggest require.resolve instead of path.resolve, but yeah, thanks! 👍

@cyyyu
Copy link

cyyyu commented Sep 30, 2017

It doesn't work for me but thanks anyway!

Should use require.resolve as @facekapow metioned.

@ozanmuyes
Copy link

Suppose there's no way to avoid caching as it seems. requireing and then invalidating is a two step process...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment