16

In my electron app I have installed sqlite3 via npm

npm install sqlite3

But once i try to interact with the database it cant find the database, here is the log:

Uncaught Error: Cannot find module 'D:\play\electron-quick-start\node_modules\sqlite3\lib\binding\electron-v1.3-win32-x64\node_sqlite3.node'

Here is JS code:

console.log('whooooo');

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('../db/info.db');

db.serialize(function () {
    db.run("CREATE TABLE 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();

I also try in this way:

npm install sqlite3 --build-from-source

but it fails to install!

Also, i am using Python3. How do you install a module to work with electron?

rakibtg
  • 5,521
  • 11
  • 50
  • 73

6 Answers6

37

Firstly:

npm install electron-rebuild

then try this several times:

./node_modules/.bin/electron-rebuild -w sqlite3 -p

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
jamesxiang
  • 708
  • 5
  • 10
  • works perfectly, if on windows use some bash client (I used git bash) – Ajay Dec 10 '17 at 22:18
  • second step failed, tips me 'node-gyp' have a wrong proxy, but I don't know how to change the node-gyp's proxy setting – krosshj Dec 19 '18 at 08:02
  • This worked for issue I was having with better-sqlite3 ./node_modules/.bin/electron-rebuild -w better-sqlite3 -p :) but did not work for sqlite3 :( ... I was trying out both modules and had issues getting both to work, so this at least allowed me to move forward using one of them. Thxs – jsherk Jan 09 '19 at 18:43
  • this did it on macos catalina for me – techshack Nov 03 '19 at 16:44
9

You have to build this native module with Electron based configurations.

Try:
1. cd node_modules/sqlite3
2. npm run prepublish
3. node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
4. node-gyp rebuild --target=1.3.1 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64

This is assuming you have the very latest version of electron. You can change the config to match your electron version.

Adi
  • 5,560
  • 1
  • 24
  • 37
  • 1
    Worked like a charm, thanks, remember if you don't have node gyp, install it before do this things.. – Paulo Rodrigues Sep 16 '16 at 18:24
  • 1
    It's not working for me. Any solution for this error? Error is : Uncaught Error: Cannot find module 'path_to_project\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node' – Jay Dec 27 '16 at 17:58
  • http://stackoverflow.com/questions/41307807/i-am-getting-an-error-while-connecting-to-sqlite3-database is there any solution for this one – Jay Dec 27 '16 at 19:04
  • 2
    I ended up with the error `npm ERR! missing script: prepublish` – Jacob Nelson Feb 21 '19 at 09:45
  • Fails at step #2 - `Missing script: "prepublish"` – Coder Oct 31 '22 at 01:11
8

If none of these are working try this.

npm install electron-builder

Add this in your script tag of package.json file

 "postinstall": "electron-builder install-app-deps"

Then execute this

npm run postinstall 

This saved a lot of my time

Shahabaz
  • 663
  • 1
  • 10
  • 25
3

1: Include rebuild in Package.json file and install npm electron-rebuild

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "rebuild": "electron-rebuild -f -w sqlite3"
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "author",
  "license": "CC0-1.0",
  "devDependencies": {
    "@types/file-saver": "0.0.1",
    "electron": "1.7",
    "electron-rebuild": "^1.6.0"
  },
  "dependencies": {
    "sqlite3": "^3.1.13"
  }
}

2: install python 2.7 and add its path to environmental variable e.g C:\Python27;

3: npm INSTALL and then npm run rebuild

  • +1 for ("rebuild": "electron-rebuild -f -w sqlite3"), it's solved the problem for me after running the command "npm run rebuild" in terminal – Boris Sep 30 '19 at 12:47
2

You have just installed the sqlite3 module but you need to rebuild it to run on a specific platform. You'll need electron-rebuild package to rebuild the binary.

Run Command npm i --save-dev electron-rebuild from your project directory.After Installing the ˚electron-rebuild. Run the following command to build sqlite3 binary for your platform.

./node_modules/.bin/electron-rebuild -w sqlite3 -p

If rebuild fails, run npm install and then run the above-given command once again.

Pini Cheyni
  • 5,073
  • 2
  • 40
  • 58
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
  • this doesn't work for me. It tells me '.' is not recognized as an internal or external command, operable program or batch file. – Frustrated programmer Aug 08 '19 at 04:52
  • go to /node_modules/.bin/ and open terminal there. run command "electron-rebuild -w sqlite3 -p" – Kiran Maniya Aug 08 '19 at 07:14
  • Well it originally failed cuz I didn't have python 2.7, after I installed 2.7 I got this error https://pastebin.com/xmhsG85A – Frustrated programmer Aug 09 '19 at 01:24
  • You have to install windows build tools, vc++ and python before start building the sqlite3 module. Download and install VC++ 2015.3 v140 toolset (x86,x64) – Kiran Maniya Aug 09 '19 at 06:07
  • 1
    Took a while to download the build tools, but afterwards, I re-ran the cmd and it worked :D already tested some of SQLite's features and everything seems to be working. TYSM! I just about gave up trying to use SQLite in electron cause nothing else worked! – Frustrated programmer Aug 13 '19 at 19:08
  • if you are using sqlite3 with electron, i suggest you to use http://knexjs.org/ Knex Module. I buit 3 application on top of it and it's too easy to maintain database with it. – Kiran Maniya Aug 14 '19 at 05:06
0

For cross compilation try sqlite3-offline or sqlite3-offline-next packages wich are working out of the box

mbesson
  • 629
  • 5
  • 24