-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
module: add clearCache for CJS and ESM #61767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1d0accc
b3bd79a
0507308
ba8ffaa
af0f7d0
83d2402
ee06977
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,57 @@ const require = createRequire(import.meta.url); | |
| const siblingModule = require('./sibling-module'); | ||
| ``` | ||
|
|
||
| ### `module.clearCache(specifier[, options])` | ||
|
|
||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| > Stability: 1.1 - Active development | ||
|
|
||
| * `specifier` {string|URL} The module specifier or URL to resolve. The resolved URL/filename | ||
| is cleared from the load cache; the specifier (with `parentURL` and `importAttributes`) | ||
| is cleared from the resolve cache. | ||
| * `options` {Object} | ||
| * `mode` {string} Which caches to clear. Supported values are `'all'`, `'commonjs'`, and `'module'`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this isn't a very good choice as an option. For resolution, There's also the question of what happens when the same
The latter two again re-introduces the dual package hazard. IMO for loading cache, it's better to always just clear them all and don't even leave an option for partial clears unless anyone specifically ask for them. Also, while 2 and 4 seems silly, the current way of mixing the resolution with loading in the API can very easily trick you into them. Splitting the two APIs helps avoid unintentional partial clears. |
||
| **Default:** `'all'`. | ||
| * `parentURL` {string|URL} The parent URL used to resolve non-URL specifiers. | ||
| For CommonJS, pass `pathToFileURL(__filename)`. For ES modules, pass `import.meta.url`. | ||
| * `importAttributes` {Object} Import attributes for ESM resolution. | ||
| * Returns: {Object} An object with `{ commonjs: boolean, module: boolean }` indicating whether entries | ||
| were removed from each cache. | ||
|
|
||
| Clears the CommonJS `require` cache and/or the ESM module cache for a module. This enables | ||
| reload patterns similar to deleting from `require.cache` in CommonJS, and is useful for HMR. | ||
| When `mode` is `'all'`, resolution failures for one module system do not throw; check the | ||
| returned flags to see what was cleared. | ||
| This also clears resolution cache entries for that specifier. Clearing a module does not clear | ||
| cached entries for its dependencies, and other specifiers that resolve to the same target may | ||
| remain. | ||
| When a `file:` URL is resolved, cached module jobs for the same file path are cleared even if | ||
| they differ by search or hash. | ||
|
|
||
| ```mjs | ||
| import { clearCache } from 'node:module'; | ||
|
|
||
| const url = new URL('./mod.mjs', import.meta.url); | ||
| await import(url.href); | ||
|
|
||
| clearCache(url); | ||
| await import(url.href); // re-executes the module | ||
| ``` | ||
|
|
||
| ```cjs | ||
| const { clearCache } = require('node:module'); | ||
| const path = require('node:path'); | ||
|
|
||
| const file = path.join(__dirname, 'mod.js'); | ||
| require(file); | ||
|
|
||
| clearCache(file); | ||
| require(file); // re-executes the module | ||
| ``` | ||
|
|
||
| ### `module.findPackageJSON(specifier[, base])` | ||
|
|
||
| <!-- YAML | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it more -what's the point of clearing the resolve cache? For the purpose of HMR, I think clearing the load cache is usually enough, and many user-land solutions never seem to care about the resolve cache. Even in the examples below, it doesn't demonstrate clearing the resolve cache. I think it's better to either split them into two APIs, or add an option that allows users to control which one to clear.