'How to use the npm module gs1-barcode-parser?
I want to extract the price value from the GS1 data martix QR code value using Nodejs. Using the module
npm i gs1-barcode-parser
Tried the below throwing "parseBarcode" is not a function
const { parseBarcode } = require('gs1-barcode-parser');
let barcode = '\u001d01093393871222863922001405\u001d1522030631030006691095751410';
console.log(parseBarcode(barcode));
Solution 1:[1]
I would use the modded module gs1-barcode-parser-mod2
const parser = require("gs1-barcode-parser-mod2")
let barcode = ']C101040123456789011715012910ABC1233932978471131030005253922471142127649716';
console.log(parser.parseBarcode(barcode));
Unfortunately your barcode seems to be invalid. You will need to decode from UTF-8 as that would result in ?01093393871222863922001405?1522030631030006691095751410
even then your barcode seems to have a missing prefix, called an Application Identifier or for short, AI (]xxxxx..
).
A valid barcode example is given in the code snippet above.
Solution 2:[2]
So your trying to import a module to a CJS environment, but the module is written to resolve as a front end JavaScript Module.
Fortunatly the package contains a single source file, with a single IIFE function.
THIS WILL MAKE IT AN EASY FIX!
Please note though, it is only one file & one function, but the file, and the function are really big for being a single file & a single function, so big, that it wouldn't be impracticable to add them as a snippet in a Stack Overflow answer, consequently; I have created a repository, and added the source code, that works as a solution for this problem, to the repository. I have outlined the changes that one needs to make to the module, to get it to work on the backend (in the Node REPL).
The files I added to my REPO should work for your current needs though. Continue Reading
To Convert The Module you Need to do the Following...
What you need to do is convert the package single source file into a Node module, which is very easy. The file is far to long to add to a Stack Overflow answer. So what I have done, is I have added the file into a public GitHub repository located HERE.
Go to the link to the file I rewrote
- The file is a module conversion of this file.
- Look at both files so you can see how I made the change.
In a nutshell, the author of the module wrote it using whats called an
IIFE: Immediately Invoked Function Execution
Its a type of function that is invoked immediately. The purpose of the function is to invoke at the very start of a script being loaded. IFFE's load before anything else, which is why he was using it to load his frontend module.
The Entire Module is One function, and the whole thing is wrapped like the example below:
(function (){
// Function Logic Here.
})();
Basically, what I did, is I unwrapped it, and appended a module.export.BarcodeParser
assignment to the function.
To reiterate: Go Get the new File (or technically its written as a CJS module) from the Repo I Created
At This Point, Just Follow the Steps Below.
Create a new Node.js project, and make sure you execute
npm init
to generate a properly formattedpackage.json
file.Test the converted module: To test the file w/ the changes JayD3V implemented: Create a file in the same directory as the barcode parser, and add the following script to it.
const BarCodeParser = require('./BarcodeParser.js');
let barcode = ']C101040123456789011715012910ABC123?39329784711?310300052539224711?42127649716';
console.log(BarCodeParser.parseBarcode(barcode));
- Then execute the file using the
node
command.
The README.md document in the repository has much of the information I added here in it.
Here is what it looked like when I ran it:
Solution 3:[3]
you're using it on server-side (backend)
https://www.npmjs.com/package/gs1-barcode-parser?msclkid=bb2e3c05cf5111ecabd6ce6b8aeb965a
as its documentation says it is a library it also says that in order to use gs1-barcode-parser you have to add it in your application (on client side / frontend) as below:
<script src="./src/BarcodeParser.js"></script>
then you can use single function provided by it (parseBarcode
) as follow :
try {
var barcode = document.getElementById("barcode").value,
answer = parseBarcode(barcode);
// do something ...
} catch (e) {
alert(e);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jesse de gans |
Solution 2 | |
Solution 3 | ht006 |