Hacker News new | past | comments | ask | show | jobs | submit login

Only barely relevant, but this seems like as good a place to ask as any: is anyone here in a position to explain why it not possible to reload an imported ES2016 module at runtime?

In the old 'require' days this was easy: delete it from the cache and then re-require. But AFAICT there is no equivalent[0] for ES2016 modules, and weirder still, no one seems to miss it or request it as a feature. This is surprising - I'd think people would want to, say, add something to a module and then have a running node application that imports that module pick up the change without restarting the node app.

0: It is possible, but only by doing hacky things to fool the cache in to thinking it's a new file - e.g. make a symlink to the module file and dynamically import the symlink, or for an http import, add an unnecessary parameter to the url.




Because you were never really reloading it, you were loading a second copy of it (and praying the old one gets garbage collected). "Delete it from the cache" doesn't stop timers, clean up event listeners, remove references held in memory, etc. It just removes the reference to the module that the require method holds. You were never reloading in the first place, just swapping out a reference. Arguably, this was hackier than the hacks you suggest now, since it's less explicit that you might not be accomplishing what you hope to.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: