Embedding Node.js ESM #52432
Unanswered
segevfiner
asked this question in
General
Replies: 1 comment
-
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
-
The current embedding API gives you to ability to run a CJS script (That seems to need to bootstrap
require
by itself), or to get a callback with arunCjs
function, but what's the right way to run ESM when embedding? The current sample https://nodejs.org/docs/latest-v20.x/api/embedding.html usesrunInThisContext
which doesn't support dynamic import by default withoutvm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER
which dumps an experimental warning, andvm.SourceTextModule
is experimental, which I'm not sure how to correctly enable when embedding, and also seems incomplete such as requiring alink
callback 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