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 formatted package.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:
