May 30, 2015

Using node_sqlite3 with Electron

electron-logo

At the moment, node_sqlite3 doesn’t want to play nicely with Electron apps. This is likely to change, however none of the methods shown in the Electron documentation worked for me. I was able to finally piece together a workaround until the module is natively supported.

The problem begins after installing sqlite3 and attempting to use it in your electron project. A runtime error will indicate that node_sqlite3.node can’t be found. You’ll need to look closely at this error because it will indicate what module version electron is expecting. The error will show a missing path of something like node_modules/sqlite3/lib/binding/node-v47-darwin-x64 but your install will have a path of something like node_modules/sqlite3/lib/binding/node-v14-darwin-x64 (note electron is looking for v47, but we have v14 installed).

Make a note of the required module version (v47 in this case) because you’ll need it below.

Also make a note of what version of electron you are running (electron -v). In this case I’m running version 0.36.1.

As indicated on the electron support page, node-gyp can be used to re-build the package, however the documentation doesn’t seem to work as-is, which may be specific to sqlite3 but I’m not sure. The following does seem to work.

If you are running a different version of electron then you’ll need to replace 0.36.1 and v47 with the appropriate version numbers in the commands below. This command is also specific to OSX. If you are not running on OSX then you will need to change target_platform and target_arch to the correct values for your system (for example windows is win32 instead of darwin).  The parameter values are listed on the node-pre-gyp info page.

cd node_modules/sqlite3
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v47-darwin-x64
node-gyp rebuild --target=0.36.1 --arch=x64 --target_platform=darwin --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/node-v47-darwin-x64

For convenience, I have the following scripts in my package.json file so that this happens automatically:

"scripts": {
"postinstall": "npm run rebuild-sqlite3",
"rebuild-sqlite3": "cd node_modules/sqlite3 && npm run prepublish && node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v47-darwin-x64 && node-gyp rebuild --target=0.36.1 --arch=x64 --target_platform=darwin --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/node-v47-darwin-x64"
},

21 Comments on “Using node_sqlite3 with Electron

b3kart
July 14, 2015 at 12:03 pm

You’re f*cking amazing man. Thanks for hours saved.

Reply
yail
July 15, 2015 at 7:09 pm

thanks you are a genius!!!

Reply
Razvan
November 30, 2015 at 6:38 am

Brilliant! I managed to get it working using your instructions. Thanks for that :)

Reply
Chris
December 1, 2015 at 4:27 pm

Thank you SO MUCH, I have literally been struggling with this for hours and this FINALLY worked for me. You’re the best!

Reply
José Mariñas
December 2, 2015 at 10:44 pm

Thanks very much for the solution, but have a comment.
when I run:
npm run prepuplish

I have the next error:
npm ERR! missing: nan@~2.1.0, required by sqlite3@3.1.1
….
….
….

I see nan in node_modules but i have that error.

I can solve that error doing a npm install in node_modules/sqlite3 before the npm run prepuplish

I don’t know if this solution is fine but I can use sqlite3 now.

A final question I can use the sqlite3 on the main process but in render process I have the next error:

Uncaught TypeError: Cannot read property ‘_ansicursor’ of undefined

Can I use Sqlite3 in the render process?

Reply
Jason
December 2, 2015 at 11:39 pm

Hmm, not sure about those other errors. Maybe they could help you on over at https://github.com/mapbox/node-sqlite3/issues

Reply
Petru Dimitriu
December 6, 2015 at 3:31 pm

Hi! I stumbled on much the same error you encountered and this is how I got over it:
I saw that it was complaining about a missing nan@~2.1.0 , so I just ran npm install nan@~2.1.0 , then I proceeded with the next commands, and it worked. Hope this helps anyone.

Reply
rowan
December 9, 2015 at 3:43 am

This is such a good write up, thanks Jason!

For anyone who’s wondering what a windows-ish implementation looks like its something like this

node-gyp configure –module_name=node_sqlite3 –module_path=../lib/binding/node-46-win32-x64

and

node-gyp rebuild –target=0.35.4 –arch=x64 –target_platform=win32 –dist-url=https://atom.io/download/atom-shell –module_name=node_sqlite3 –module_path=../lib/binding/node-v46-win32-x64

Reply
Jason
December 10, 2015 at 8:35 pm

nice – thanks for that!

Reply
Maximilian Ruppert
December 19, 2015 at 8:54 pm

Thanks a lot for your windows version. I tried it and noticed on little error (at leaste on my win 7 machine).
I had to use –module_name / –module_path (reads – -) instead of –module_name / –module_path when I copied and tried your code.

I also took to make a step by step instruction of all your very helpful input, that helped me integrate sqlite3 into my bachelor’s thesis project:

https://gist.github.com/maximilian-ruppert/9de273f72c1ba4aa62d6

Reply
Loilo
December 17, 2015 at 7:42 am

Great stuff, saved me a lot of time.
Note: I had latestet node.js version installed but I had to upgrade my node-gyp manually to make this work.
I did that with the help of a nice little summary on how to do that for node-newbies like me:
https://github.com/nodejs/node-gyp/wiki/Updating-npm's-bundled-node-gyp

Reply
Casey
December 26, 2015 at 11:34 pm

Awesome, thanks for this!

Reply
Drew
February 1, 2016 at 2:51 pm

I’m not having as much luck with your tutorial.

I get this error after running the last command (npm-gyp rebuild): https://gist.github.com/DrewWeth/117a61f479dfbd0694ea

Any advice?

Reply
Jason
February 10, 2016 at 12:18 pm

Hmm I haven’t seen that one unfortunately. Maybe somebody on the node-gyp repo can see what’s going on there?

Reply
Hardik
February 1, 2016 at 11:22 pm

Hi there.

What about using it cross-platform ?

We have to change the scripts platform wise. But in package.json, I find no option to do that.

I want to run it on Mac, Linux and windows.

Please help.

Thanks.

Reply
Jason
February 15, 2016 at 12:41 pm

Had the same issue, but found that it happens because the npm install for sqlite is using the version of node on your system, which may differ from the version of node that electron is using. For example, my system version was 0.4.10, and electron was using 5.1.1. You can change your system version using n, then do the npm install, and electron should start up without complaints.

Reply
Jason
February 15, 2016 at 4:18 pm

Hey Jason that sounds like a solution to the root problem rather than the band-aid that I have been using. Let me ask you how do you find what version electron is using, where are those files located? I also thought electron was using io.js which causes a bit of further confusion, but anyway…

Reply
Jason
February 16, 2016 at 10:49 am

You can find it by putting “document.write(process.versions.node)” in index.html. We kind of stumbled across it going through the quick start tutorial (http://electron.atom.io/docs/latest/tutorial/quick-start/).

One thing to remember with this is that it will still pull from the npm repo based on your OS. For example, if you are on OSX and want to build for Windows, you’ll have to do an npm pull on a Windows machine and pull in that sqlite directory manually into your node_modules in your repo.

Reply
Jason
February 16, 2016 at 6:43 pm

That’s very useful info. I’m in the middle of doing a few test builds and documentation but I’m going to give this a closer look and see if I can get it working using that approach. Thanks for sharing!

Reply
dcrysatlj
March 11, 2016 at 3:19 pm

genius!!!

Finally i can use SQL database with electron

Reply
Hamdi Fourati
April 8, 2016 at 9:57 am

i had the same error and a little hack did the work for me , change the folder ../lib/binding/node-v47-win32-x64 to ../lib/binding/electron-v0.37-win32-x64 .
always check the versions of both node and electron.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *