opencv-js icon indicating copy to clipboard operation
opencv-js copied to clipboard

[node.js] Error thrown at opencv.js:30 with all the module code printed out to the console

Open toniopelo opened this issue 2 years ago • 2 comments

While using this package on node, I would have errors thrown in what appeared to be a "random" fashion, without any concrete explanations in the error. It would just output the whole opencv.js module minified code and fail.

This kind of errors while using opencv.js followed me for a while before I took the time to go into a deep investigation of the root cause. I will post as a comment below my explanation and what worked for me for others in my situation.

toniopelo avatar Jul 07 '23 08:07 toniopelo

Explanation:

  • opencv.js is built with emscripten v2.0.10 (see here) with the arguments as stated in this build script
  • This produces a opencv.js script where opencv is wrapped in a emscripten surrounding code that allow the actual code to be executed. This wrapper do several things, among these things, it adds this code: image This opencv.js code was formatted with prettier to be more readable.
  • These two process.on() calls grab all the uncaughtExceptions and unhandledRejections you code can throw and re-throw or exit the process.

This is what cause these apparently random errors with no details. The original error probably comes from elsewhere in your codebase, but this is handled here by this emscripten wrapper and re-thrown without details.

Solution: After several hours of rebuilding the opencv.js module with a newer version of emscripten, tweaking the build arguments and so on, I gave up this approach because it failed at runtime for reasons I choose to not investigate due to time constraints (even though I managed to produce several other versions of opencv.js that didn't contain the process.on() calls). I finally ended up by just using my own version of the opencv.js module and removing the process.on() calls from it. I used the same version that this package use, but I don't use this package anymore so I can remove these faulty lines from it. I used mirada as it is done here to have the typescript definitions. I finally added a process.on('uncaughtException', () => {...}) elsewhere in my codebase to handle properly these errors woth proper logs and all.

I'll keep this issue open as the package is still having this issue and my solution is more a workaround.

toniopelo avatar Jul 07 '23 08:07 toniopelo

Thank you for the solution! This is really annoying, makes development almost impossible on any project that uses this library.

Moebytes avatar Aug 25 '24 04:08 Moebytes