I'm developing a Node-API module together with an Electron application. The N-API module is running in the render process of Electron, since it has a pretty complex API, that would be hard to get through a context bridge, and I'm only planning on running local resources anyway. However, none of the printing to stdout done by the N-API module is visible anywhere.
I've tried listening to the process.stdout, which fails because "The _read() method is not implemented":
process.stdout.on("data", (data) => console.log(data));
Piping also doesn't work, because "Cannot pipe, not readable":
const duplexStream = new Stream.Duplex();
duplexStream.on("data", (data) => console.log(data));
process.stdout.pipe(duplexStream);
I even tried overriding the stdout.write() method, which actually worked for calls from within JavaScript, but still didn't show anything from the native module:
process.stdout.write = (data) => {
console.log(data);
return true;
};
So is there any way to view the stdout/stderr of a Node-API module from within the script?
Try enabling ELECTRON_ENABLE_LOGGING environment variable from https://www.electronjs.org/docs/latest/api/environment-variables#electron_enable_logging and check also ELECTRON_LOG_FILE
Electron seems to do something with the stdout in the renderer, I can't find it at the moment but I suppose that it is on purpose.
Try doing this instead of printf in your C++ code:
// Do this only once per instance initialization
// and then destroy it when exiting
auto console_log = Napi::Persistent(env.Global().Get("console").ToObject()
.Get("log").As<Napi::Function>());
// Do this to print
console_log.Call({Napi::String::New(env, "Displayed through JS")});