Files
30-seconds-of-code/snippets/requireUncached.md
2020-09-15 21:52:00 +03:00

21 lines
432 B
Markdown

---
title: requireUncached
tags: 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.
```js
const requireUncached = module => {
delete require.cache[require.resolve(module)];
return require(module);
};
```
```js
const fs = requireUncached('fs'); // 'fs' will be loaded fresh every time
```