# OpenCV.js does not work from a web worker if WASM threads are enabled

**URL:** <https://forum.opencv.org/t/opencv-js-does-not-work-from-a-web-worker-if-wasm-threads-are-enabled/18010>\
**Category:** Uncategorized\
**Tags:** opencvjs, wasm\
**Created:** [June 20, 2024, 5:52am UTC](https://forum.opencv.org/t/opencv-js-does-not-work-from-a-web-worker-if-wasm-threads-are-enabled/18010 "2024-06-20T05:52:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andynewman](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@andynewman](https://forum.opencv.org/u/andynewman)\
**Post date:** [June 20, 2024, 5:52am UTC](https://forum.opencv.org/t/opencv-js-does-not-work-from-a-web-worker-if-wasm-threads-are-enabled/18010/1 "2024-06-20T05:52:28Z")

</div>

OpenCV.js does not work for me when built with WASM threads enabled (`--threads` option active) and used in a web worker.

The problem occurs when using OpenCV.js compiled with emscripten (3.1.57, or latest)

```auto
$ git clone https://github.com/opencv/opencv.git
$ git checkout 4.10.0

```

Now build opencv.js with, eg. emscripten 3.1.57 (or latest emscripten version ; I tried a few versions, all produce the same result):

```auto
$ emcmake python3 ./opencv/platforms/js/build_js.py build_wasmthreads --build_wasm --threads

```

opencv.js has been generated in the `build_wasmthreads/bin` folder.

Next, make sure SharedArrayBuffer is enabled regardless of cross-origin header status, to be on the safe side ; for this, I use `--enable-features=SharedArrayBuffer` on Chrome.

Now try the following:

Case 1)

- load opencv.js in a web page using a `<script>` tag
- `await cv;` This line of code will succeed.

Case 2)

- load opencv.js in a web worker using `importScripts()`.
- `await cv;` This line of code will fail with the following error:

> Refused to execute script from ‘[http://localhost/undefined](http://localhost/undefined)’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled

Note that OpenCV.js will always work fine when WASM threads are disabled (–threads option not used).

Shall a specific emscripten version be used for WASM threads to work in web workers?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 20, 2024, 7:53am UTC](https://forum.opencv.org/t/opencv-js-does-not-work-from-a-web-worker-if-wasm-threads-are-enabled/18010/2 "2024-06-20T07:53:23Z")

</div>

Looks like you’ve got the problem pinned down.

I’d recommend filing an issue on OpenCV’s github, or adding your findings if such a one already exists.
