Files
30-seconds-of-code/snippets/requireUncached.md
Angelos Chalaris 1ad5d1004d Add requireUncached
2020-08-07 15:49:39 +03:00

428 B

title, tags
title tags
requireUncached node,advanced

Loads a module after removing it from the cache (if exists).

Use delete to remove the module from the cache (if exists). Use require() to load the module again.

const requireUncached = module => {
  delete require.cache[require.resolve(module)];
  return require(module);
};
const fs = requireUncached('fs'); // 'fs' will be loaded fresh every time