How to use sqlite3 module with electron?
By far the easiest way to use SQLite with electron is with electron-builder
.
First, add a postinstall step in your package.json:
"scripts": {
"postinstall": "install-app-deps"
...
}
and then install the necessary dependencies and build:
npm install --save-dev electron-builder
npm install --save sqlite3
npm run postinstall
electron-builder will build the native module for your platform, with the correct name for the Electron binding; and you can then require
it in code as normal.
See my github repo and blog post - it took me quite a while to figure this out too.
I would not recommend the native node sqlite3 module. It requires being rebuild to work with electron. This is a massive pain to do - At least I can never get it to work and their a no instructions to for rebuilding modules on windows.
Instead have a look at kripken's 'sql.js' module which is sqlite3 that has been compiled 100% in JavaScript. https://github.com/kripken/sql.js/
Two aspects are to be considered here:
- Setting
NODE_PATH
: this lets electron know where to find your modules (see this answer for a thorough explanation) - Compiling native modules against electron headers: see official docs
And checkout the following questions, that ask the same thing:
- Electron App with Database
- Using NodeJS plugins in Elelectron
My tip would be to give lovefield (by Google) a try.
I was having same problem. Tried everything and atlast this worked for me :-
npm install --save sqlite3
npm install --save electron-rebuild
npm install --save electron-prebuilt
.\node_modules\.bin\electron-rebuild.cmd
This will create "electron-v1.3-win32-x64" folder in .\node_modules\sqlite3\lib\binding\ location which is used by electron to use sqlite3.
Just start application and you will be able to use sqlite3 now.
npm install --save sqlite3
npm install --save-dev electron-rebuild
Then, in the scripts of your package.json, add this line:
"scripts": {
"postinstall": "electron-rebuild",
...
},
Then just re-install to trigger the post-install:
npm install
Works flawlessly for me in a complex use case also involving electron-builder, electron-webpack and sequelize.
It works in electron-webpack's dev mode and in production mode for both Windows and Linux.