I'm trying to create a database on the client-side using sql.js and vanilla js. I guess, there is some problem within the installation process. I've done the 'npm install sql.js' and included the CDN. I get this error from the console 'Uncaught ReferenceError: require is not defined'
(BTW this my first question on here, open to suggestions...)
This is pretty much the expression shown in the documentation on their website. I can't make it work, though. Is it because of node.js or something else?
<script type="module">
const initSqlJs = require('/sql.js');
const SQL = await initSqlJs({
locateFile: (file) =>
'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.js',
});
const db = new SQL.Database();
let sqlstr =
"CREATE TABLE hello (a int, b char); \
INSERT INTO hello VALUES (0, 'hello'); \
INSERT INTO hello VALUES (1, 'world');";
db.run(sqlstr);
</script>
"require" is when you use Node.
As you using vanilla JS, you should use this, as explained in the github repository (line 3, commented)
const initSqlJs = window.initSqlJs;