Skip to content
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

Important feature: Allow busting ESM cache #38322

Closed
izelnakri opened this issue Apr 20, 2021 · 6 comments
Closed

Important feature: Allow busting ESM cache #38322

izelnakri opened this issue Apr 20, 2021 · 6 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js.

Comments

@izelnakri
Copy link

izelnakri commented Apr 20, 2021

Currently ESM modules are cached separately for ESM imports and dynamic imports. https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#esm_no_require_cache

There is currently no way to remove the ESM cache for specific files/modules in node.js/JavaScript context. I"m 100% sure about this unless there is some weird undocumented way. I even tried the following to bust the cache and it doesnt work:

import { createRequire } from 'module';

export function runTests(testFiles) {
  const require = createRequire(import.meta.url); // NOTE: Here I'm creating `require` reference on ES module

  // ....

  Object.keys(require.cache).forEach((module) => delete require.cache[module]); // This clears the cache object but its not used by ESM
  
   // Then import the test modules directly serves cache
}

This missing feature prevents writing ESM based javascript test runners because test files need to be re-evaluated without cache on file changes. In other words, cache busting for ESM modules is an important feature that node.js should provide somehow.

@izelnakri izelnakri changed the title Allow busting ESM cache Important feature: Allow busting ESM cache Apr 20, 2021
@Ayase-252 Ayase-252 added esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. labels Apr 21, 2021
@benjamingr
Copy link
Member

@nodejs/modules

@devsnek
Copy link
Member

devsnek commented Apr 21, 2021

this is a duplicate but I can't find the other issue on my phone. tldr is either append some sort of unique id to the url each time you want to import it (foo.mjs?v=1) or bug v8 about getting proper hot reloading support.

@izelnakri
Copy link
Author

Would be great if we document foo.mjs?v=1 behavior in the API docs, will try it out today. Thanks!

@giltayar
Copy link
Contributor

As usual, here's a link to my article on implementing mocking modules, which use the above technique: https://dev.to/giltayar/mock-all-you-want-supporting-es-modules-in-the-testdouble-js-mocking-library-3gh1

@izelnakri
Copy link
Author

izelnakri commented Apr 21, 2021

This works, thank you @devsnek and @giltayar , we should really document this in the API docs: https://nodejs.org/api/esm.html#esm_no_require_cache . Closing this issue now

@icetbr
Copy link

icetbr commented Jun 4, 2024

This landed on node 22: --experimental-require-module. It allows mocha --watch to work with ESM modules, which was broken feature due to require.cache. Maybe it can help you out.

Here is the person who found the "fix"
https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/

The docs
https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require

Here is where I first saw the merge
#51977 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

6 participants