Files
30-seconds-of-code/snippets/requireUncached.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

23 lines
508 B
Markdown

---
title: requireUncached
tags: node,advanced
firstSeen: 2020-08-07T15:49:39+03:00
lastUpdated: 2020-09-15T16:28:04+03:00
---
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
```