Dynamic Uncached Import (Feature Request)
What is the problem this feature would solve?
Imagine this setup:
file a.ts
setInterval(() => {
import(`./b.ts?id=${Date.now()}`).then(console.log)
}, 1000)
file b.ts
import c from './c.ts'
export default c
file c.ts
const myString = 'abc'
export default myString
Now when running this will console log the result of dynamic import, and THIS IS IMPORTANT the dynamic import has a query param which means Bun should import a fresh version each time
But if while this script runs you go to c.ts and change the string manually the console log will not change, since C inside B is cached which is supper bad since I have a query param in B and I expect a fresh version of it and it's child imports also should be not cached for this exact import
Well that obsiously is kind of inconvenient, because that literally means you can't load user code since in runtime since it always will give the same result even if user manually changed this
Now I understand that this is not a bug, it is how ESM is designed to work and is the same in Node, Bun and Deno And below is my proposal
What is the feature you are proposing to solve the problem?
What I'm proposing is a new API maybe called uncachedImport which will import that module/file and it's child imports in a uncached manier basically fresh versions each time the method is called
What alternatives have you considered?
There is require.cache which you can use to remove modules from cache, but it's not clear how to remove the child imports and I tried it I'm not even sure that it plays nicely with both import/require , and overall seems like a bad practice and couldn't make it working in Bun with both import/require
This is the last feature that prevents me from switching my app to bun. My backend manages and cordinates a lof of apis (each responsible for specific purposes). When I need to update an API, I don't have to reload the whole app. I just need to request the backend to reload (uncache) that specific API. I'm currently using "require.cache" to solve this problem.
- added: Bun does work in most cases, but getting error randomly when trying to reload an api, I'm unable to track down the cause yet.
I've also encountered this issue.
I am watching files for update using fs.watch (https://bun.sh/guides/read-file/watch)
Then I do "const { default: newFn } = await import(filePath)"
Unfortunately, I've noticed newFn is always the same, which defeats the whole purpose of fs.watch in this case
How did you solve it? @xd1gital
https://github.com/nodejs/node/issues/49442
To clear the import cache for a particular file, use delete require.cache[specifier]. This applies to ESM & CJS modules, and the next import/require reloads again
Hey, This doesn't work For require Cache is just empty always, Thats one thing And second is how do you find out what are the children's of another import
With your approach you can delete only imports of first level which you know their specifier but wat if you do not know which imports are in the children imports, these will be still imported from cache You think of one level import, but there are many nested imports, In my case and what this feature request is saying is that all children imports would be fine to uncache