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
H
Heitor44d ago
did you run npm install in your project folder?
M
Matt44d ago
Yes I did. My script tag also has module attribute
H
Heitor44d ago
oh, i tested it with this in the first line and it worked, maybe try it var Quagga = require('quagga').default;
M
Matt44d ago
Same issue Which commands did you run? @Heitor is this because I'm running node in browser without express?
H
Heitor44d 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
M
Matt44d ago
Hmm I'm lost right now keep getting this error ReferenceError: document is not defined when running node scan.js
M
Matt44d ago
No description
M
Matt44d ago
I have node
M
Matt44d ago
No description
M
Matt44d 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.
M
Matt44d 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.
H
Heitor44d ago
maybe try changing the require to ES6 module import import Quagga from 'quagga';
M
Matt44d ago
@Heitor What npm commands did you run?
H
Heitor44d ago
just npm i quagga and before i started the project, with npm init -y
M
Matt44d ago
hmm ive no idea lol do you have node isntalled?
H
Heitor44d ago
yeah i have node, not sure what is happening there, sorry i could not help much 😕
M
Matt44d ago
No worries! Thank you @Heitor
F
firxworx38d 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
More Posts
Nested route problemI have a nested route system, One is in App.js and one in RoomGuard component The app.js has router Max width with fr in grid.Hi, I am trying to limit breakout in the grid to a amx width but would like to retain ints atributesopen full stackNot sure how useful this will be but it looks useful for understanding fullstack. Trigger warning: rchange default keyboard layout without changing the default language in windows 11windows 11 has plenty of flaws, and one of them is how badly it accommodates people who speak multipHaving problem in wrapping custom hooks with React componentThis is my code:- const App = () => { return ( <Router> <Routes> <RoFind new positions of elementsPlease watch the following video, in question 5 when I connect the answers together they are followiPosition Sticky stops working, after I set a sibling element to Position Absolute```html <header> <div class="logo"></div> <div class="header__links"></div> <nav class="nav"><question regarding "key" in jsIk this might seem a lil dumb but can someone explain to me why we use "key" in js or react as wellEven/odd row styling with CSS Grid and subgrid, is this a good idea?For work, I needed some table styling and decided to use CSS Grid. Because I need even/odd row stylihow to prevent last item of flex to grow```css section {display:flex; flex-wrap:wrap; gap:1rem} div {background-color:red; width:0; min-widtResponsive IdeasHello there! Thank you in advance for your help. I've been working on a Weather API that I need to fIssue in starting sassFirst off, i have exisiting .css file doing some mini project of mine then i encountered limitation Button coloring with a imaged backgroundIt's proving quite difficult to find the right color to make this not look out of place/tacky.. I tHelp me, help you, by helping me, so I don't ask so many questions(yep?)Here's the site: https://anglian-online.netlify.app/ Here's the code: https://github.com/callum-lainFor some reason the post value isn't being shown even tho in var_dump it shows that it's there.You can see that the `$_POST["ajaxMonth"]` has a string value of "2" in the first image however whenResponsive Carousel (TailWind + JS)Hello, I'm trying to build a responsive carousel. Ideally, what I'd like to happen is that the amouWebpage CSS to make it be better on varying display size (responsive)tell me where to start how to fix this , where to study this ,on varying display somethings get stacIs my site layout saveable?Two years ago I made a custom layout for my website. At a small scale it worked perfectly (only one simulating support for missing web apis?Is there a way to simulate *the lack of support* for web apis in Firefox or Chrome? Like for exampleGoogle search result name changeChanged the domain name recently from infotelligent to selling.com. I have changed the all the title