I want to develop desktop app using electron that uses sqlite3 package installed via npm with the command
npm install --save sqlite3
but it gives the following error in electron browser console
Uncaught Error: Cannot find module 'E:\allcode\eapp\node_modules\sqlite3\lib\binding\node-v45-win32-x64\node_sqlite3.node'
My development environment is windows 8.1 x64 node version 12.7
my package.json file looks like this:
{
"name": "eapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron-prebuilt": "^0.32.1"
},
"dependencies": {
"angular": "^1.3.5",
"sqlite3": "^3.1.0"
}
}
index.js file
var app = require('app');
var BrowserWindow = require('browser-window');
require('crash-reporter').start();
var mainWindow = null;
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadUrl('file://' + __dirname + '/index.html');
mainWindow.openDevTools();
mainWindow.on('closed', function() {
mainWindow = null;
});
});
my.js file
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');
db.serialize(function() {
db.run("CREATE TABLE if not exists lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
index.html file
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div >
<div>
<h2>Hello</h2>
</div>
</div>
<!--<script src="js/jquery-1.11.3.min.js"></script>-->
<script src="js/my.js"></script>
</body>
</html>
I would not recommend the native node sqlite3 module. It requires being rebuild to work with electron. This is a massive pain to do - At least I can never get it to work and their a no instructions to for rebuilding modules on windows.
Instead have a look at kripken's 'sql.js' module which is sqlite3 that has been compiled 100% in JavaScript. https://github.com/kripken/sql.js/
A simpler solution:
npm i electron-rebuild --save-dev
./node_modules/.bin/electron-rebuild
(or.\node_modules\.bin\electron-rebuild.cmd
on windows)PS: v47 is my version, be careful to choose the good one (in your case v45)
I was having same problem. Tried everything and atlast this worked for me :-
This will create "electron-v1.3-win32-x64" folder in .\node_modules\sqlite3\lib\binding\ location which is used by electron to use sqlite3.
Just start application and you will be able to use sqlite3 now.
Two aspects are to be considered here:
NODE_PATH
: this lets electron know where to find your modules (see this answer for a thorough explanation)And checkout the following questions, that ask the same thing:
My tip would be to give lovefield (by Google) a try.
I encounter this error too. Here is how i solve it:
npm install --save-dev electron-rebuild
then:./node_modules/.bin/electron-rebuild
from: https://electronjs.org/docs/tutorial/using-native-node-modules
ps: While it's on rebuilding, don't use
npm start
to lanch the electron app. Otherwise the rebuild process would fail.Have a look at a similar answer here
TL;DR