You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
createRequire does not respect deleting module from require.cache #57696
For context, this is after I provided source via a custom sync loader which had both resolve and load hooks. I traced the source all the way to this point, and Node.js does not take into account that I had provided a custom source. That's why I consider this a bug.
Also, I'm using a cache busting query already, but it gets stripped away a few functions up when turned into a filename for the sake of requiring it via CJS resolution, so it's apparently ignored.
@ljharb right but it's a weird hybrid of require and import, because it's in an ESM project and it's an ESM file being imported, but require is called via createRequire, which goes through the hassle of resolving it as if required, but skips require's actual resolution which would use require.cache (Module._cache), and ultimately finds an ESM module, sees that it's already been resolved, and uses that one's exports. Ironically, it's really only using require-logic to skip the cache busting, but in every other area in this code it's acting as if it's ESM. So it's inconsistent. Either it should fully use ESM resolution and see the cache busting query, or it should fully use require-logic and see that I deleted it from require.cache.
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
This issue has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
Activity
sdegutis commentedon Mar 31, 2025
For context, this is after I provided source via a custom sync loader which had both resolve and load hooks. I traced the source all the way to this point, and Node.js does not take into account that I had provided a custom source. That's why I consider this a bug.
sdegutis commentedon Apr 1, 2025
Also, I'm using a cache busting query already, but it gets stripped away a few functions up when turned into a filename for the sake of requiring it via CJS resolution, so it's apparently ignored.
ljharb commentedon Apr 2, 2025
require only takes file paths or bare specifiers, not URLs, so no query strings would make sense there.
sdegutis commentedon Apr 2, 2025
@ljharb right but it's a weird hybrid of require and import, because it's in an ESM project and it's an ESM file being imported, but require is called via createRequire, which goes through the hassle of resolving it as if required, but skips require's actual resolution which would use require.cache (Module._cache), and ultimately finds an ESM module, sees that it's already been resolved, and uses that one's exports. Ironically, it's really only using require-logic to skip the cache busting, but in every other area in this code it's acting as if it's ESM. So it's inconsistent. Either it should fully use ESM resolution and see the cache busting query, or it should fully use require-logic and see that I deleted it from require.cache.
ljharb commentedon Apr 2, 2025
I’m confused, if you want to skip the cache busting just import it normally. The only place it makes any sense to require ESM is inside a CJS file.
sdegutis commentedon Apr 2, 2025
@ljharb In this case, I'm using createRequire because it's a sync ESM module (no tlawait) and I'd like to avoid poisining all sync methods involved
ljharb commentedon Apr 2, 2025
I’m still confused - what do you mean by poisoning?
sdegutis commentedon Apr 2, 2025
https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/
sdegutis commentedon Apr 2, 2025
Interesting, that article gives me a new respect for Go.
ljharb commentedon Apr 2, 2025
ahhh this is something that can't be a static import at the root of the file?
sdegutis commentedon Apr 2, 2025
@ljharb Right, it's dynamically imported/required in a sync context.
jdmarshall commentedon Aug 16, 2025
require('./foo.mjs') also does not honor cache invalidation.
Anyone have a workaround? I'm trying to migrate a module to ESM while providing backward compat and this is breaking tests.
github-actions commentedon Apr 20, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
jdmarshall commentedon Apr 22, 2026
I have a trick but it only works when importing ESM not CJS, by basically adding a query parameter
github-actions commentedon Jul 23, 2026
This issue has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
jdmarshall commentedon Jul 23, 2026
This is still an important case particularly for unit testing frameworks.