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`
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.