Skip to content

Commit

Permalink
Update uncached-module-require
Browse files Browse the repository at this point in the history
Turn into a question, rework SEO.
  • Loading branch information
Chalarangelo committed May 14, 2024
1 parent df01172 commit c9130a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
9 changes: 6 additions & 3 deletions content/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,6 @@
- from: /js/s/redirect
to: /js/s/redirect-to-url
status: 301!
- from: /js/s/require-uncached
to: /js/s/uncached-module-require
status: 301!
- from: /js/s/attempt
to: /js/s/attempt-invoking-function
status: 301!
Expand Down Expand Up @@ -2911,3 +2908,9 @@
- from: /css/s/triangle
to: /css/s/shapes
status: 301!
- from: /js/s/require-uncached
to: /js/s/reload-module
status: 301!
- from: /js/s/uncached-module-require
to: /js/s/reload-module
status: 301!
24 changes: 24 additions & 0 deletions content/snippets/js/s/reload-module.md
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
```
22 changes: 0 additions & 22 deletions content/snippets/js/s/uncached-module-require.md

This file was deleted.

0 comments on commit c9130a1

Please sign in to comment.