Electron app cant find sqlite3 module
Asked Answered
O

7

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?

Overmuch answered 2/8, 2016 at 9:26 Comment(1)
You got any solution for this?Lollis
B
38

Firstly:

npm install electron-rebuild

then try this several times:

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

Buckner answered 13/9, 2016 at 6:14 Comment(4)
works perfectly, if on windows use some bash client (I used git bash)Charlenacharlene
second step failed, tips me 'node-gyp' have a wrong proxy, but I don't know how to change the node-gyp's proxy settingHomeric
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. ThxsLagasse
this did it on macos catalina for meDoggerel
M
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.

Mobcap answered 2/8, 2016 at 11:58 Comment(5)
Worked like a charm, thanks, remember if you don't have node gyp, install it before do this things..Drusi
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'Lollis
#41308307 is there any solution for this oneLollis
I ended up with the error npm ERR! missing script: prepublishLevona
Fails at step #2 - Missing script: "prepublish"Muscadel
W
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

Whitmer answered 12/11, 2019 at 17:0 Comment(1)
Didn't work. I get the same error. I am using Ubuntu 20.04.Muscadel
P
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

Pardue answered 28/11, 2017 at 12:30 Comment(1)
+1 for ("rebuild": "electron-rebuild -f -w sqlite3"), it's solved the problem for me after running the command "npm run rebuild" in terminalMonto
S
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.

Stranglehold answered 7/8, 2019 at 16:50 Comment(6)
this doesn't work for me. It tells me '.' is not recognized as an internal or external command, operable program or batch file.Venal
go to /node_modules/.bin/ and open terminal there. run command "electron-rebuild -w sqlite3 -p"Stranglehold
Well it originally failed cuz I didn't have python 2.7, after I installed 2.7 I got this error pastebin.com/xmhsG85AVenal
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)Stranglehold
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!Venal
if you are using sqlite3 with electron, i suggest you to use knexjs.org Knex Module. I buit 3 application on top of it and it's too easy to maintain database with it.Stranglehold
N
0

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

Neuroma answered 13/6, 2023 at 10:31 Comment(0)
T
0

Could be as simple as; I tested many things on this page. this worked.

npm add sqlite3
Talus answered 3/2 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.