Embedding Node.js ESM #4607
Unanswered
segevfiner
asked this question in
Q&A
Replies: 0 comments 1 reply
-
|
I worked around that by doing something like that in the warmup script: globalThis._loadModuleESM = async function (source, url) {
const {loadESM} = require('internal/process/esm_loader');
let result = null;
return loadESM((loader) => {
result = loader.eval(source, url).catch((e) => {
/* Wrap non-native errors to simplify error-detection. */
if (!(e instanceof Error)) e = new Error(e);
return e;
});
return result;
}).then(() => result);
};Then you can retrieve It's not the best, but at least it works. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The current embedding API gives you to ability to run a CJS script (That seems to need to bootstrap
requireby itself), or to get a callback with arunCjsfunction, but what's the right way to run ESM when embedding? The current sample https://nodejs.org/docs/latest-v20.x/api/embedding.html usesrunInThisContextwhich doesn't support dynamic import by default withoutvm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADERwhich dumps an experimental warning, andvm.SourceTextModuleis experimental, which I'm not sure how to correctly enable when embedding, and also seems incomplete such as requiring alinkcallback without giving you access to the default resolution algorithm, and so on... And the CJS main execution is sync, so if youimport, how would you wait for the resulting promise to finish from embedding code?Beta Was this translation helpful? Give feedback.
All reactions