-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
30 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Can I reload a module in Node.js? | ||
shortTitle: Reload a module | ||
type: question | ||
language: javascript | ||
tags: [node] | ||
cover: tea-laptop-table | ||
excerpt: Ever wanted to reload a module in Node.js? Here's how you can do it. | ||
dateModified: 2024-05-13 | ||
--- | ||
|
||
If you've ever tried to **reload a module** in Node.js, you might have noticed that it's not as straightforward as you might expect. **Node.js caches modules** after they are loaded, so subsequent `require()` calls return the cached module instead of loading it again. This can be useful for **performance** reasons, but it can also be a hindrance when you want to reload a module. | ||
|
||
Luckily, the require cache can be accessed via `require.cache`, allowing you to manipulate it as needed. Simply using `delete`, you can **remove a module from the cache**, if it exists, allowing you to **load it fresh** the next time you call `require()`. | ||
|
||
```js | ||
const requireUncached = module => { | ||
delete require.cache[require.resolve(module)]; | ||
return require(module); | ||
}; | ||
|
||
const fs = requireUncached('fs'); | ||
// 'fs' will be loaded fresh every time | ||
``` |
This file was deleted.
Oops, something went wrong.