Skip to content

Instantly share code, notes, and snippets.

@craigvantonder
Last active October 16, 2022 05:47
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save craigvantonder/f59277cd788f8aa755e3bdbe5d21f08e to your computer and use it in GitHub Desktop.
Save craigvantonder/f59277cd788f8aa755e3bdbe5d21f08e to your computer and use it in GitHub Desktop.
Electron SQLite3 Integration

Electron SQLite3 Integration

When trying to use the node-sqlite3 module in Electron I got the error:

Error: Cannot find module '/path/to/my/application/node_modules/sqlite3/lib/binding/electron-v1.4-linux-x64/node_sqlite3.node'

Using Ubuntu 16.04 with Node 7.1.0 and Electron 1.4.12.

I read the following:

And managed to solve things by:

Install node-gyp globally

npm install -g -save node-gyp

Install sqlite3

npm install --save sqlite3

Navigate into the sqlite3 module folder

cd node_modules/sqlite3

Install the modules dependencies

npm install

Prebulish the module

npm run prepublish

Check which binding exists, In this case node-v51-linux-x64

ls lib/binding/

Start compilation by setting the module path to the correct binding/version

node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v51-linux-x64

Finish compilation by setting your build to the correct target version. You will find the target version of your Electron app in the package.json file in the root folder of your Electron directory, e.g: "electron": "1.4.12".

node-gyp rebuild --target=1.4.12 --arch=x64 --target_platform=linux --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/node-v51-linux-x64

Rename the binding folder so that it is the same as the module that it could not find in the error

mv lib/binding/node-v51-linux-x64/ lib/binding/electron-v1.4-linux-x64/

Now you can use sqlite3 in your Electron app.

Further example:

node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/[DIR]
node-gyp rebuild --target=[ELEC-VER] --arch=[ARCH] --target_platform=[PLATFORM] --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/node-[NODE-VER]-[PLATFORM]-[ARCH]

Placeholder   Value
[DIR]         The name of the directory called "Can not find" in the preliminary survey
[ELEC-VER]    Version of Electron.
[ARCH]        In the case of 32Bit ia32
              In the case of 64Bit x64
[PLATFORM]    In the case of Windows win32
              In the case of Mac darwin
              In the case of Linux linux
[NODE-VER]    ls lib/binding/ => in this case `node-v51-linux-x64`
@jonataswalker
Copy link

Definitely the only way I got this thing working.

Thanks for sharing this!

@craigvantonder
Copy link
Author

@jonataswalker Pleasure :-)

@RobertRajcool
Copy link

its really help us

@pdbq21
Copy link

pdbq21 commented Apr 18, 2018

It's help me, thanks

@andonov85
Copy link

Perfection, its help a lot. I am with node-v57-linux-x64, Ubuntu 17.10 and Electron 2.0.4. Only to mention prepublish now is prepublishOnly and you need to install (for Ubuntu if "gyp ERR! stack Error: not found: make") -> "sudo apt-get install build-essential" before run "node-gyp rebuild". Thanks a lot! :)

@cloverich
Copy link

In addition to the guide above, electron-rebuild will do this for you. I run it in my postinstall npm script, and manually any time I change my electron version.

Also the tl;dr of what's happening here: Packages like sqlite3 use compiled binaries, and these must be compiled for the environment where they are used. With Electron, its using a (potentially) different version of node than your locally installed node version, so you must compile sqlite3's binary for its environment.

@fmacedoo
Copy link

fmacedoo commented Aug 9, 2019

Great! Worked nicely.

I would change --module_path=../lib/binding/node-[NODE-VER]-[PLATFORM]-[ARCH] to --module_path=../lib/binding/electron-[NODE-VER]-[PLATFORM]-[ARCH] to avoid the last MOVE step. But i didn't test that way.

@DavidRiosAlex
Copy link

this works!!!!! even in november 2019 ! ty so much you save me from days and days searching a solution ! I was going to install python-shell to use python scripts but no anymore, ty again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment