Skip to content

Unable to build projects using opencv.js #6710

@pmalacho-mit

Description

@pmalacho-mit

Describe the bug

In short: I'm looking for a way to bundle OpenCV.js in my SvelteKit project (which utilizes Vite). It's not clear to me if it's OpenCV.js's issue, or something on the Vite end, but I've exhausted all my resources in trying to figure out the issue described below. Any help would be much appreciated!

Less short:
OpenCV produces a javascript version of their plugin: OpenCV.js

Taking a look at the source, it seems like the authors must've intended the plugin to work nicely in both node and browser. Unfortunately, it seems some of their attempts to make the library flexible have caused trouble when including it in a project bundled/built by Vite.

I'm specifically trying to use an npm pacakge that simply wraps the source, @techstark/opencv-js (NOTE: Other similar packages exist, which have similiar issues).

Without any changes to the default SvelteKit / Vite config, the project builds with no errors but cannot be loaded (i.e. If i navigate to the port, the browser endlessly hangs).

If I fight with the config enough (mainly using resolve.alias to handle issues of the opencv.js 'requiring' "fs", "path", and "crypto"), I can get the page to load, but it will throw an Uncaught ReferenceError: Module is not defined error, as can be seen in the Reproduction below.

It seems the prevailing wisdom and assumption is that, if OpenCV.js is going to be bundled, it needs to be done so with Webpack (this is what @techstark/opencv-js's author recommends, and what I have personal success doing). I'm hoping that that won't have to be the case, so I can happily bundle OpenCV.js in with my SvelteKit projects.

Thank you!

Reproduction

https://stackblitz.com/edit/vitejs-vite-ijha1v?file=main.js

System Info

System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 435.54 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.13.2 - ~/project-path/local-node-install/node/bin/node
    Yarn: 1.22.17 - ~/npm/bin/yarn
    npm: 8.1.2 - ~/project-path/local-node-install/node/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Firefox: 96.0.3
    Safari: 14.1.2

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions