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?

1
  • You got any solution for this?
    – Jay
    Jan 2, 2017 at 19:52

6 Answers 6

38

Firstly:

npm install electron-rebuild

then try this several times:

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

4
  • works perfectly, if on windows use some bash client (I used git bash)
    – Ajay
    Dec 10, 2017 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, 2018 at 8: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 9, 2019 at 18:43
  • this did it on macos catalina for me
    – techshack
    Nov 3, 2019 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.

5
  • 1
    Worked like a charm, thanks, remember if you don't have node gyp, install it before do this things.. Sep 16, 2016 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, 2016 at 17:58
  • stackoverflow.com/questions/41307807/… is there any solution for this one
    – Jay
    Dec 27, 2016 at 19:04
  • 2
    I ended up with the error npm ERR! missing script: prepublish Feb 21, 2019 at 9:45
  • Fails at step #2 - Missing script: "prepublish"
    – Coder
    Oct 31, 2022 at 1: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

1
  • Didn't work. I get the same error. I am using Ubuntu 20.04.
    – Coder
    Oct 31, 2022 at 1:12
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
  • +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, 2019 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.

6
  • this doesn't work for me. It tells me '.' is not recognized as an internal or external command, operable program or batch file. Aug 8, 2019 at 4:52
  • go to /node_modules/.bin/ and open terminal there. run command "electron-rebuild -w sqlite3 -p" Aug 8, 2019 at 7:14
  • Well it originally failed cuz I didn't have python 2.7, after I installed 2.7 I got this error pastebin.com/xmhsG85A Aug 9, 2019 at 1: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) Aug 9, 2019 at 6: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! Aug 13, 2019 at 19:08
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.