Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron app cant find sqlite3 module

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?

like image 794
rakibtg Avatar asked Aug 02 '16 09:08

rakibtg


4 Answers

Firstly:

npm install electron-rebuild

then try this several times:

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

like image 93
jamesxiang Avatar answered Nov 11 '22 01:11

jamesxiang


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.

like image 32
Adi Avatar answered Nov 11 '22 01:11

Adi


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

like image 7
Shahabaz Avatar answered Nov 11 '22 02:11

Shahabaz


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

like image 3
Muhammad Waseem Avatar answered Nov 11 '22 00:11

Muhammad Waseem