11

I understand you can clear files from cache while using CommonJS simply by deleteing from require.cache and requireing the file again. However I've been unable to find an equivalent option when using ESM. Is this possible, and if so how?

2 Answers 2

6
+25

Reading at esm issues it seems cache is intentionally not exposed.

I'm afraid the answer to your question is simply no.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems there is a hack using dynamic imports:

const modules = {
  moduleA: async () => await import(`./module-a.js?v=${Date.now()}`)
}

Then use it like:

async function myTest() {
    // module will be reset at each call of the function
    const moduleA = await modules.moduleA() 
}

Unfortunately, like said by @lisonge, the memory of the older module will not be freed up, so keep in mind that it can lead to a memory leak in case of intensive usage.

Here is an issue about it with further details on that technique there

2 Comments

The old version modules that were loaded before were unable to remove from memory. Your solution has made the memory occupy more and more under extreme circumstances
Thanks @lisonge for pointing this out, I updated my answer to reflect that 👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.