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"
}
}
{
"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");
}
});
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, an advanced barcode-reader written in JavaScript
QuaggaJS is an advanced barcode-reader written in JavaScript
18 Replies
Heitor
Heitor•10mo ago
did you run npm install in your project folder?
Matt
MattOP•10mo ago
Yes I did. My script tag also has module attribute
Heitor
Heitor•10mo ago
oh, i tested it with this in the first line and it worked, maybe try it var Quagga = require('quagga').default;
Matt
MattOP•10mo ago
Same issue Which commands did you run? @Heitor is this because I'm running node in browser without express?
Heitor
Heitor•10mo ago
not sure, I installed the library with npm, created a file index.js with the example and ran "node index.js" in the terminal
Matt
MattOP•10mo ago
Hmm I'm lost right now keep getting this error ReferenceError: document is not defined when running node scan.js
Matt
MattOP•10mo ago
No description
Matt
MattOP•10mo ago
I have node
Matt
MattOP•10mo ago
No description
Matt
MattOP•10mo ago
GeeksforGeeks
How to run Node Server ? - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Matt
MattOP•10mo ago
Following these exact commands + sample code still gives me errors
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
Heitor
Heitor•10mo ago
maybe try changing the require to ES6 module import import Quagga from 'quagga';
Matt
MattOP•10mo ago
@Heitor What npm commands did you run?
Heitor
Heitor•10mo ago
just npm i quagga and before i started the project, with npm init -y
Matt
MattOP•10mo ago
hmm ive no idea lol do you have node isntalled?
Heitor
Heitor•10mo ago
yeah i have node, not sure what is happening there, sorry i could not help much 😕
Matt
MattOP•10mo ago
No worries! Thank you @Heitor
firxworx
firxworx•10mo ago
@Matt quagga is a browser library per its readme so your issue makes sense. Your error gives it away: any time you see a mention of document not existing you are definitely trying to run something written for browser (where there's a document global e.g. document.querySelector("#heroSection")) in a server-side environment (like NodeJS) where no such thing exists. Hence the error: code is trying to reference it and its not defined.
Want results from more Discord servers?
Add your server