Closed as not planned
Description
Details
I am trying to use the NodeJS Embedder API in a C++ application. My goal would be to load JS modules that might contain import
statement. What would be the best way to go about that?
So far, I have created a v8::Module
and I can instantiate it and run it without trouble. However, I have issue importing modules especially default ones such as path
. I am not sure what would be the best way to resolve module recursively taking into account the internal modules nodejs exposes (path
, http
, etc...)
What would be the best way to achieve something like that?
Node.js version
v18.10.0
Example code
Example with custom module execution:
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate,
"import Path from 'path';console.log(Path);"
).ToLocalChecked();
v8::ScriptOrigin origin(isolate, v8::String::NewFromUtf8(isolate, "main.mjs").ToLocalChecked(),
0,
0,
false,
0,
v8::Local<v8::Value>(),
false,
false,
true
);
v8::ScriptCompiler::Source source(source, origin);
v8::Local<v8::Module> module;
if (!v8::ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
return;
}
module->InstantiateModule(context, [](
v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
v8::Local<v8::FixedArray> import_assertions, v8::Local<v8::Module> referrer) {
// How should the module be resolved? Can we use the ModuleWrapper from NodeJS?
return v8::Local<v8::Module>();
});
Operating system
MacOS Monterey 12.6, but not that relevant for this particular question
Scope
Embedder API
Module and version
Not applicable.