Abhranil Majumder
Abhranil Majumder

Reputation: 43

Connecting electron with sqlite3

I want to connect electron with sqlite3. My package.json file is listed below

{
  "name": "electrontest2",
  "version": "1.0.0",
  "description": "",
  "main": "db.js",
  "scripts": {
    "start": "electron ."
  },

"scripts": {
"start": "electron ."
},
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron-prebuilt": "^1.2.2"
  },
  "dependencies": {
    "jquery": "^2.1.4",
    "sqlite3": "^3.1.4"
  }
}

But while doing npm start it is throwing this error

App threw an error during load
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

I think I am getting this error as i am having :

'/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/node-v46-linux-x64/node_sqlite3.node'

instead of

'/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'

Please tell where I am making mistake. Currently while running the app it is throwing the error listed above.

Upvotes: 4

Views: 3268

Answers (4)

Nihal Singh Varia
Nihal Singh Varia

Reputation: 23

First add new postinstall script in your package.json

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

Then install it using : npm install --save-dev electron-builder npm install --save sqlite3 npm run postinstall

You can also find the same at : Electron application SQLITE package has not been found installed

Upvotes: 0

Steve Melia
Steve Melia

Reputation: 1469

You're correct that the error is because the compiled SQLite module is named for Node by default; and thus Electron can't find it. Using node-gyp to rebuild with the correct name, as in the answer above should work; but it's a lot easier to use the npm package electron-builder which will give you a better workflow and work with multiple platforms and versions without having to add scripts for node-gyp.

See this answer to a similar question which details how to get it working.

Upvotes: 2

Ultranuke
Ultranuke

Reputation: 1895

I tried running this with the latest Node version (6.2.2), if you are using an older version eg. the 4.2 then you will get an older bundle instead of the v48 your electron will need:

npm install sqlite3 --build-from-source

And simply renamed the folder in node_modules/sqlite3/lib/binding from node-v48-linux-x64 to electron-v1.2-linux-x64.

It started complaining about requiring callbacks were they are supposed to be optional, but I just added them like this and it worked:

var stmt = db.prepare(
  "INSERT INTO discount VALUES (?, ?, ?)",
  [1,2,3],
  () => true //Expected callback that is supposed to be optional
);

I just needed the Sqlite in my electron app for simple purposes and didn't have any other problems in my implementation.

Upvotes: 1

LuisPinto
LuisPinto

Reputation: 1697

Try rebuilding the sqlite3 package as follows:

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

Check what is you electron version

electron -v

Replace --target value with the vesion of your electron package.

Upvotes: 1

Related Questions