Closed Mozzart88 closed 2 months ago
cc @nodejs/loaders
It's not a bug, require
ing ES modules is not possible, you need to use import()
.
Clearing loaded ES modules is also not possible because the ECMAScript spec does not define any way to clear modules.
You could load several instances of the same module by using query string, e.g. import('./file.js')
and import('./file.js?test=0')
would load two module instances of the same file.js
file.
Great, Thnx!
But I still don't understand the usage of the createRequire
function - if require
is not possible in ES, then why do we need this function and how should we use it?
createRequire
is provided as an escape mecanism for apps that relies upon a legacy feature of the CJS loader. It's not meant to be used unless you have no other choice.
Ok, but it's not working properly. Or did I use it incorrectly? In my case, it just throws an error.
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.
Details
Hello! For a testing purposes I need to dynamically load and unload modules. My solution is looks like this
This usage is documented here
And it works fine with
tsx
, but not withnode
. With node it throws an exceptionERR_REQUIRE_ESM
with this message:Node.js version
v 18.15 v 20.15.0
Example code
No response
Operating system
MacOS Ventura 13.4.1 and on docker
Scope
code, runtime, meta (don't really understand this question)
Module and version
node:module