Tutorial example crashes in “Using OpenCV.js In Node.js”

System information (version)

Tested both
OpenCV 4.3.0-219-g713577b257
and
OpenCV 3.4.10-161-gfd06139c20

Node.JS 12.16 and 10.14
Tested both on Mac and Windows

Detailed description

I’m running the exampels on: Using OpenCV.js In Node.js
https://docs.opencv.org/master/dc/de6/tutorial_js_nodejs.html

The first three examples work just fine. The forth and last example though (Face Detection using Haar Cascades) crashes. It has some kind of file system error. The error lies in the web assembly itself and the same error occures both on Mac and Windows and both on Node.JS latest stable 12.16 and 10.14.

Steps to reproduce

I install the npm packages jsdom and canvas and retreive the files described in the example. Then run node.

Mac terminal commands:

mkdir test
cd test
npm i jsdom canvas
curl https://docs.opencv.org/3.4/opencv.js -O
curl https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml -O
curl https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_eye.xml -O
curl https://docs.opencv.org/3.4/lena.jpg -O
touch exampleNodeCanvasData.js
# Copy and paste the file content from the web page (Face Detection using Haar Cascades example)
# https://docs.opencv.org/3.4/dc/de6/tutorial_js_nodejs.html
node exampleNodeCanvasData.js

Should produce a crash dump with the following error message:

abort(ReferenceError: existsSync is not defined). Build with -s ASSERTIONS=1 for more info.
(Use `node --trace-uncaught ...` to show where the exception was thrown)

If I run node --trace-uncaught exampleNodeCanvasData.js

RuntimeError: abort(ReferenceError: existsSync is not defined). Build with -s ASSERTIONS=1 for more info.
    at process.abort (/Users/tornord/Projects/Nodejs/test/test200511/opencv.js:31:15465)
    at process.emit (events.js:310:20)
    at processPromiseRejections (internal/process/promises.js:209:33)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)
tornord@Tors-Air test200511 %% node --trace-uncaught  exampleNodeCanvasData.js
(node:35693) UnhandledPromiseRejectionWarning: ReferenceError: installDOM is not defined
    at /Users/tornord/Projects/Nodejs/test/test200511/exampleNodeCanvasData.js:67:5
    at new Promise (<anonymous>)
    at loadOpenCV (/Users/tornord/Projects/Nodejs/test/test200511/exampleNodeCanvasData.js:66:10)
    at /Users/tornord/Projects/Nodejs/test/test200511/exampleNodeCanvasData.js:3:9
    at Object.<anonymous> (/Users/tornord/Projects/Nodejs/test/test200511/exampleNodeCanvasData.js:46:3)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
(node:35693) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:35693) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I also built a version 4.3 web assembly on my own and replaced the opencv.js file. Version 4.3.0-219-g713577b257-dirty. Got the same crash:

RuntimeError: abort(ReferenceError: existsSync is not defined). Build with -s ASSERTIONS=1 for more info.
    at process.abort (/Users/tornord/Documents/test/opencv.js:31:15465)
    at process.emit (events.js:310:20)
    at processPromiseRejections (internal/process/promises.js:209:33)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)

1 possible answer(s) on “Tutorial example crashes in “Using OpenCV.js In Node.js”

  1. The example will work if line 3 is changed to:

    const { writeFileSync, existsSync, mkdirSync } = require("fs");
    

    I can put it in a PR