Using Sequelize with sqlite3 inside an Electron app

Update 8th of May 2020

The method below may not be needed anymore. All you have to do now is to use electron-builder and place a short script in your package.json.

npm install --save-dev electron-builder

And then add the following code in your package.json scripts:

"postinstall": "electron-builder install-app-deps"

Introduction

A few months ago I ran into some problems when I tried to use Sequelize with sqlite3 inside an Electron app. Even though I installed everything properly, I was getting this error:

Uncaught Error: The dialect sqlite is not supported. (Error: Please install sqlite3 package manually)

I was able to fix this using this website but it appears to be down now. I decided to write down the method here and also adding a bit more details on how to solve the problem.

Solution

Go to node_modules/sequelize/lib/dialects/sqlite/connection-manager.js and identify the MODULE_NOT_FOUND error code. Log the error with a console.log(err) before the error is thrown.

Run the application and you will get a more detailed error:
{ Error: Cannot find module 'C:\Users\user\Documents\devStuff\work\app\server\node_modules\sqlite3\lib\binding\electron-v4.1-win32-x64\node_sqlite3.node'

Note the electron-v4.1-win32-x64. You might have something different depending on the electron version you installed

Update and run the following commands to make this work:

cd node_modules/sqlite3 && npm install nan

node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v4.1-win32-x64

node-gyp build --target=4.1.4 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v4.1-win32-x64

--module_path replace with what you got from the MODULE_NOT_FOUND error
--target_platform win32, darwin or linux
--arch x64 or ia32
--target the version of electron you are using

If there are issues on Windows about MSB4019, check this issue https://github.com/brianmcd/contextify/issues/96

That's all

By following the steps above I managed to make the configuration work on Windows and Mac. Hopefully this solves the problem for you too.

If you still encounter the problem, check this post on stackoverflow. It offers multiple approaches.