111

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>
statox
  • 2,827
  • 1
  • 21
  • 41
manas
  • 6,119
  • 10
  • 45
  • 56

10 Answers10

145

By far the easiest way to use SQLite with electron is with electron-builder.

First, add a postinstall step in your package.json:

"scripts": {
   "postinstall": "install-app-deps"
   ...
}

and then install the necessary dependencies and build:

npm install --save-dev electron-builder
npm install --save sqlite3
npm run postinstall

electron-builder will build the native module for your platform, with the correct name for the Electron binding; and you can then require it in code as normal.

See my github repo and blog post - it took me quite a while to figure this out too.

Alex Beals
  • 1,965
  • 4
  • 18
  • 26
Steve Melia
  • 1,479
  • 1
  • 9
  • 5
  • 2
    Using your [electron-boilerplate-sqlite](https://github.com/sjmelia/electron-boilerplate-sqlite) is indeed the easier method, but with `npm install` – Bernardo Ramos Feb 24 '17 at 08:22
  • 1
    when run 'npm run postinstall', i got this error "Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch." – chirag Sep 25 '18 at 08:55
  • This has saved my hide not once, but twice so far! – John Nesbitt Feb 04 '19 at 15:43
  • Does anyone know whether the `--save sqlite3` option can be reliably tagged onto the first line (with `--save-dev electron-builder`), where `npm install` would only be run once? – ballade4op52 Aug 16 '19 at 17:21
  • This does not work for me, I get errors like ` ../../nan/nan_object_wrap.h:67:18: error: ‘class Nan::Persistent’ has no member named ‘MarkIndependent’` – Michael Sep 09 '19 at 01:09
  • 2
    The easiest way to use sqlite3 with electron apps is to follow sqlite3 documentation here : https://www.npmjs.com/package/sqlite3#custom-builds-and-electron. Works for me on electron 8.3.0, no rebuild required. I'm just giving more visibility to Sailab Rahi's answer. – Shayana Keiraved Pahal May 17 '20 at 19:47
  • I installed these dependencies and now nom run start does not work anymore. – maxischl May 05 '21 at 14:23
  • 4
    Shouldn't it be `"postinstall": "electron-builder install-app-deps"` ? – Maciej Krawczyk Jul 15 '21 at 15:09
21

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/

Joue Bien
  • 267
  • 2
  • 5
  • 4
    Electron docs are pretty clear that in order to use bundled native modules, you are required to rebuild them in order to work with electron. Either use the electron-rebuild utility which works most of the time or manually set the gyp flags: http://electron.atom.io/docs/tutorial/using-native-node-modules/ – Bret Feb 06 '17 at 18:57
  • mate if you can point me in the direction of the windows version of the rebuild documentation along with how to get the builder hooked up to VS compiler that would be grate. As it stands I can't find any documentation that exists, is easy to find or easy to follow. – Joue Bien Feb 20 '17 at 07:56
  • 1
    Sorry for the slow response. https://github.com/electron/electron-rebuild is a handy tool for rebuilding in development. https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build describes a multi-platform build strategy for production builds. Jut remember, once you introduce native deps, you lose the ability to cross compile for different OSs. https://github.com/nodejs/node-gyp#installation has decent docs on necessary windows compiler tools. – Bret Mar 16 '17 at 21:17
  • 1
    none of these comments are relevant to this answer! – user3791372 May 21 '17 at 00:14
  • 25
    It should be mentioned that sql.js can not operate on the filesystem. Everytime you need to change the database you have to write the whole thing to disk. Making it pretty much useless for most scenarios. – mode777 Jun 14 '17 at 17:32
  • so they have now fixed a number of issues with re-compiling modules on windows. They added Visual Studio 2015 C/C++ compiler support to node-gyp so you don't need to find, configure and install visual studio 2010 and mSoft released the 2015 compiler as a standalone install. See https://github.com/nodejs/node-gyp/issues/629#issuecomment-153196245 for more info. – Joue Bien Aug 01 '17 at 04:38
  • But it explicitly says: "If you are building a native application in javascript (using Electron for instance), or are working in node.js, you will likely prefer to use a native binding of SQLite to javascript." – Post Self Oct 08 '17 at 09:21
  • 4
    sql.js its more like a toy, made for fun. It cannot be better than NeDB and other nosql databases, because its store all database in memory. So there is no good reason to use it. For small database use nosql like NeDB, for larger you have to compile sqlite – Daimos Oct 22 '17 at 20:21
  • I would assume that one can use sql.js and keep a dump of the database in the local storage or even remotely, which is still great. – Alen Siljak Apr 03 '19 at 07:53
  • also just to mention sql.js is useless if you rely on the sqlcipher extension to sqlite – Michael Sep 09 '19 at 01:03
12

Two aspects are to be considered here:

  1. Setting NODE_PATH: this lets electron know where to find your modules (see this answer for a thorough explanation)
  2. Compiling native modules against electron headers: see official docs

And checkout the following questions, that ask the same thing:


My tip would be to give lovefield (by Google) a try.

Community
  • 1
  • 1
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
10
npm install --save sqlite3
npm install --save-dev electron-rebuild

Then, in the scripts of your package.json, add this line:

"scripts": {
  "postinstall": "electron-rebuild",
  ...
},

Then just re-install to trigger the post-install:

npm install

Works flawlessly for me in a complex use case also involving electron-builder, electron-webpack and sequelize.

It works in electron-webpack's dev mode and in production mode for both Windows and Linux.

nicolas-van
  • 935
  • 8
  • 13
8

I was having same problem. Tried everything and atlast this worked for me :-

npm install --save sqlite3
npm install --save electron-rebuild
npm install --save electron-prebuilt
.\node_modules\.bin\electron-rebuild.cmd

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.

Rj-s
  • 484
  • 3
  • 7
  • 17
5

A simpler solution:

  1. Install electron-rebuild npm i electron-rebuild --save-dev
  2. Launch electron-rebuild ./node_modules/.bin/electron-rebuild (or .\node_modules\.bin\electron-rebuild.cmd on windows)
  3. Go to "node_modules/sqlite3/lib/binding/" and rename the folder "electron-v0.36-darwin-x64" to "node-v47-darwin-x64"

PS: v47 is my version, be careful to choose the good one (in your case v45)

Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
  • 9
    Can you explain the third step? Why rename? – m4heshd Apr 03 '18 at 22:00
  • 1
    I don't remember exactly now but probably because the end of the path was hardcoded. It's looking for something like `node-v${version}-darwin-x64`. – Fabien Sa Apr 26 '22 at 11:55
  • 2
    Lol thank you for replying after 4 years @fabien-sa. It reminded me how much I've grown since then. Now I'm maintaining my own SQLite3 library. – m4heshd Apr 26 '22 at 17:49
5

It works for me in version 3 and 4, unfortunately NOT version 5. See the sqlite3 documentation for details: https://www.npmjs.com/package/sqlite3#custom-builds-and-electron or otherwise run the following line: npm install sqlite3 --runtime=electron --target=4.0.0 --dist-url=https://atom.io/download/electron

Sailab Rahi
  • 581
  • 1
  • 8
  • 11
2

Have a look at a similar answer here

TL;DR

cd .\node_modules\sqlite3
npm install nan --save
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
node-gyp rebuild --target=1.3.2 --arch=x64 --target_platform=win32 --dist-url=http://electron.atom.io/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
Community
  • 1
  • 1
smkndblvr
  • 91
  • 6
2

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 startto lanch the electron app. Otherwise the rebuild process would fail.

valleygtc
  • 99
  • 4
  • 4
0

You can manually build the native modules using visual studio.

  1. Download visual studio 2019.
  2. Install package "desktop development with c++". In installation details tab select "MSVC v140 - VS 2015 C++ build tools (v14.00)"
  3. Download electron-builder in your project.
  4. In package.json create a script. "scripts": { "postinstall": "install-app-deps" }

  5. then run the script.

Joel
  • 81
  • 1
  • 7