Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created June 21, 2016 15:23
Show Gist options
  • Select an option

  • Save remarkablemark/28f04d4659162c6caffe5c046a52f39c to your computer and use it in GitHub Desktop.

Select an option

Save remarkablemark/28f04d4659162c6caffe5c046a52f39c to your computer and use it in GitHub Desktop.
Reload a node module by deleting the cache reference in require.
'use strict';
/**
* Re-require a module.
*
* @param {String} path - The module path.
* @return {*} - The module export.
*/
module.exports = function rerequire(path) {
try {
// the path in the cache key must be absolute
// https://nodejs.org/docs/latest/api/globals.html#globals_require_cache
delete require.cache[require.resolve(path)];
return require(path);
} catch(error) {
throw error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment