Sqlite3 in Electron wrapper
-
Hi all,
after having solved some problems with the initial Electron configuration , I am now stuck into making Sqlite work.
I managed to install it and recompile it, it even works in the main Process but it doesn’t in the Renderer process files (i.e. the Vue Components).I first tried simply installing Sqllite with:
- npm install electron-rebuild --save-dev
- npm install sqlite3 --save-dev
- electron-rebuild -f -w sqlite3
then I tried with the steps described here and the following postinstall script
cd node_modules/sqlite3 && npm install nan && npm run prepublish && node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v48-linux-x64 && node-gyp rebuild --target=1.6.2 --arch=x64 --target_platform=linux --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.6-linux-x64
but the result is always the same: it works in the main Process, but when I try to require Sqlite through
const sqlite3 = require('sqlite3').verbose()
in the Rendered process I getERROR in ./~/sqlite3/lib/sqlite3.js Module not found: Error: Can't resolve 'node-pre-gyp' in '/home/....../node_modules/sqlite3/lib' @ ./~/sqlite3/lib/sqlite3.js 1:13-36
but
node-pre-gyp
is correctly installed under the node_modules/sqlite3/node_modules folder . Furthermore manually installing it does not help.Any suggestion?
EDIT:
There is this minimal working boilerplate but I fear it does not help us.EDIT2:
Another useful guide that I in vain followed -
Hi @Michele-Angioni , did you get to run sqlite with electron?
-
@Michele-Angioni @ffreitas It’s almost 2021, anyone found a solution to this?
I also tried the mentioned solutions and get the same error.
-
@gvorster said in Sqlite3 in Electron wrapper:
@Michele-Angioni @ffreitas It’s almost 2021, anyone found a solution to this?
I also tried the mentioned solutions and get the same error.
I tried this with Vue and Electron, so without Quasar, and got the same error.
After creating the file
vue.config.js
module.exports = { configureWebpack: { externals: { sqlite3: "commonjs sqlite3" }, }, pluginOptions: { electronBuilder: { externals: ['sqlite3'], } }, };
I can run the electron app and use sqlite3.
Is there a place in
quasar.conf.js
or other file where these settings should fix this error? -
I got sqlite3 running now with Quasar and Electron without errors and can create a database/table!
Will post a step by step instruction tomorrow, -
Using these steps I can use sqlite3 with Quasar/Electron app
quasar create hello cd hello quasar mode add electron yarn add sqlite3 yarn add electron-builder ./node_modules/.bin/electron-builder install-app-deps quasar new b sqlite
edit
boot/sqlite.js
import sqlite3 from 'sqlite3' console.log(sqlite3) const db = new sqlite3.Database('persons.db', (err) => { if (err) { console.log(err) } else { console.log('db opened') } }) db.on("error", function(error) { console.log(error); }); db.serialize(() => { db.run('CREATE TABLE if not exists persons (id int primary key, name varchar(64))') .run('delete from persons') .run(`insert into persons(id, name) values(1, 'a')`) .run(`insert into persons(id, name) values(2, 'b')`) .run(`insert into persons(id, name) values(3, 'c')`) .all('select * from persons order by id', [], (err, rows) => { rows.forEach((row) => { console.log(`${row.id} / ${row.name}`); }); }); }) db.close() export default async (/* { app, router, Vue ... } */) => { }
in
quasar.conf.js
add boot file and webpack configurationboot: [ 'sqlite' ], build: { vueRouterMode: 'hash', // available values: 'hash', 'history' extendWebpack(cfg) { // externals property does not exist yet, so must be created, // otherwise 'push' new item cfg.externals = {sqlite3: 'commonjs sqlite3'} // without this an error occurs !!! // -->>> * aws-sdk in ./node_modules/node-pre-gyp/lib/info.js, //./node_modules/node-pre-gyp/lib/publish.js and 1 other }, },
output from dev console
quasar dev -m electron