Sec-ant / barcode-detector

A Barcode Detection API polyfill that uses ZXing-C++ WebAssembly under the hood.

Home Page:https://www.npmjs.com/package/barcode-detector/v/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failed to load or decode Blob.

mengshouer opened this issue · comments

I created a minimum instance like in the example to run.

I use esbuild-register run the code

then it throws DOMException [InvalidStateError]: Failed to execute 'detect' on 'BarcodeDetector': Failed to load or decode Blob.
image

// package.json
{
  "name": "barcode",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "node -r esbuild-register index.ts"
  },
  "author": "",
  "devDependencies": {
    "esbuild": "^0.19.7",
    "esbuild-register": "^3.5.0",
    "typescript": "^5.3.2"
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "skipLibCheck": true,
    "moduleResolution": "node"
  },
  "include": ["."]
}

I don't think this package will fully work on nodejs because it relies on some DOM APIs. This package is intended for browsers.

However this issue is caused by some mistake I made here (Reference Errors) and it will be fixed soon. Thanks for raising this issue.

Hello, v2.1.3 fixed this error message. But as I said this package is intended for browsers so it still won't work in node because DOMRectReadOnly is a DOM API and it is specified in the spec https://wicg.github.io/shape-detection-api/#detectedbarcode-section.

If you don't need a spec-compliant barcode detector and want a solution in node, check out zxing-wasm

Hello, v2.1.3 fixed this error message. But as I said this package is intended for browsers so it still won't work in node because DOMRectReadOnly is a DOM API and it is specified in the spec wicg.github.io/shape-detection-api/#detectedbarcode-section.

If you don't need a spec-compliant barcode detector and want a solution in node, check out zxing-wasm

I have already tried zxing-wasm before, but there will be errors when I run it after building it, tsconfig like above

error like:

[ERR_REQUIRE_ESM]: require() of ES Module D:\path\to\node_modules\zxing-wasm\dist\full\index.js from D:\path\to\barcodeDecoder\lib\index.js not supported.

Instead change the require of D:\path\to\node_modules\zxing-wasm\dist\full\index.js in D:\path\to\barcodeDecoder\lib\index.js to a dynamic 'import("zxing-wasm")' which is available in all CommonJS modules

then I tried dynamic import, but still made an error.