Kevin Powell - CommunityKP-C
Kevin Powell - Community2y ago
28 replies
Matt

Installing a library with JS

Hello,
I'm trying to build a demo bar code scanner. I found this library:
https://serratus.github.io/quaggaJS/#node-example

I've installed Node + Quagga (library)

My dependencies are the following:
{
  "name": "bar-code-project",
  "version": "1.0.0",
  "description": "",
  "main": "scan.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node": "^21.6.2",
    "quagga": "^0.12.1"
  }
}


However, when running test script (provided in docs), I get this error ReferenceError: require is not defined at scan.js:71:14 which makes me believe I've installed wrong. Any ideas how to fix this?


Sample script ran:
var Quagga = require('quagga');

Quagga.decodeSingle({
    src: "image-abc-123.jpg",
    numOfWorkers: 0,  // Needs to be 0 when used within node
    inputStream: {
        size: 800  // restrict input-size to be 800px in width (long-side)
    },
    decoder: {
        readers: ["code_128_reader"] // List of active readers
    },
}, function(result) {
    if(result.codeResult) {
        console.log("result", result.codeResult.code);
    } else {
        console.log("not detected");
    }
});
QuaggaJS is an advanced barcode-reader written in JavaScript
Was this page helpful?