-
Notifications
You must be signed in to change notification settings - Fork 277
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
How do we completely reload/re-import modules in ES6? #2751
Comments
|
The esm cache is not exposed, and even if it were, it is not mutable. What you want is hot-reload functionality, which has been mentioned in v8:10476. |
@AqilCont - is this issue resolved? |
Not even close, there's no solution I could find for this project and I just went with complete restarts :<. If there is something new though, I would love some info. |
While researching the same subject, I came up with a hack, that works. The key point is that once an import of a file is complete, the same file won't be imported again. Use dynamic import, and attach the exported function to a variable in your running code. |
That's actually a pretty cool solution! I might actually try it. I would always prefer a native solution though :< |
It seems that there may be a problem that memory accumulates every time a changed file is reloaded. |
What about dynamic const handlers = {
a: await import(`./sample.mjs`) // note the .mjs extension, the module format
}
handlers.a() // call an exported function, for example
if (someCondition) {
handlers.a = await import(`./sample.mjs?version=${Number((new Date()))}`) // here we add a `searchParam` (could be anything) to force reimport, must change from the last inport to trigger reimport
} It's implicitly described in the docs https://nodejs.org/api/modules.html#module-caching-caveats |
Nice hack, Thank you! |
Conversely, you can just delete the cached module after have used it: [1] The imported module doesn't even have to be renamed to *.mjs. (Source) I'm currently using this technique in the entrypoint of this project to leverage hot module replacement. @aqilc Does [1] solve your problem? If so, would you mind closing this issue? |
@codespearhead That project and solution is irrelevant to the problem presented here. "require" is not a global support by ESM, and the project you referenced does not use ESM. Typescript supports ESM imports, but it converts them to CommonJS in compilation. I'm talking about projects strictly with |
@reignmaker or simply: await import(`./sample.mjs?version=${Date.now()}`);
Note: this is strange: |
FWIW, I tried @tkrotoff 's solution in Typescript and it didn't work. I got an error: |
@kpeters-cbsi works for me, i use tsx to start the node process (with type: "module" ofc), my import looks like this: import(`./module?version=${Date.now()}`) note that i am not using a file extension for the import, i tested it with .ts .js .mjs and none worked here, dynamic module works too (also without extension!): import(`./${file}?version=${Date.now()}`); not sure about the impacts on memory yet |
This landed on node 22: Here is the person who found the "fix" The docs Here is where I first saw the merge |
How do we do a complete reload of a module in ES6? I don't spot any cache we can access/delete and I haven't found anything helpful on the internet. This is crucial on some of my projects as it lets me re-import files I've edited without restarting the whole project.
I have seen some other issues mentioning methods like "hooks" but there isn't much documentation on them and I don't know how to implement them.
The text was updated successfully, but these errors were encountered: