Require and import not working for modules in Electron

Viewed 1294

I'm trying to build a desktop app for the first time, using the Electron framework, and I'm trying to use the Trilogy module. However, several errors keep popping out.

Basically, the main idea of my code is to have a button that enters a SQL database and checks whether the table "DATA" exists. (I know it's kind of lame, but I'm trying to test out the concept here so bear with me).

My main.js code is just the exact code found in the beginner tutorial, and seemed to be working fine before I imported Trilogy. The dependencies portion of my package.json was as follows:

"dependencies": {
    "electron": "^2.0.16",
    "sql.js": "^0.5.0",
    "trilogy": "^1.4.5"
},
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ."
},
"devDependencies": {}

The head of my HTML code imported the script "tri.js", and then the body of my HTML code called that with:

<button onclick="search()">Produce Table</button>
<div id="table">table</div>

The file for "tri.js" contains the following code:

require('trilogy');
const db = new Trilogy('./storage.db');

function search()
{
    var model = "THIS DOESN'T WORK";
    document.getElementById('table').innerHTML = model;
    if (db.hasModel('DATA')) {
        model = "YES";
    }
    else {
        model = "NO";
    }
    document.getElementById('table').innerHTML = model;
}

When this code is run using npm start, the outputted text in the div becomes "THIS DOESN'T WORK". I attempted to run it on my browser, but my browser wouldn't recognize "require" and "import" (I tried using both statements), with the error messages "Can't find variable require" and "Import calls expect exactly one argument", respectively. When the button is clicked, it then says "Cannot access uninitialized variable" at the line when I call the db.hasModel() function.

When I tried removing the import and require statement, and loading it straight into the HTML first, it outputs the error that "undefined is not a constructor".

Does anybody have any idea how to implement this so that I could do what I want to do? Or if not, any suggestions on how to further debug this would also be welcome. Thanks.

0 Answers
Related