Make ElectronJS work with SQLite3

I write this blog just because I’ve spent quite some time… um hum… I mean days and nights and was having a freaking headache just to make SQLite3 work with ElectronJS. I couldn’t find the solution, neither on Github nor Stackoverflow (**Peace and Love bro!). **I’ve found the solution by myself!

Image result for gif love you bro

StackOverflow, some friendship never ends….

Sqlite3 has some issues with native modules of ElectronJS, when doing simply:

npm install --save sqlite3

It simply doesn’t work! But you are here because of that I guess or out of curiosity! let’s go quickly to your needs!

QUICK NOTE: Using Mono on Linux (or Mac) is enough, skip the Windows part and jump to step 6.


Steps

  1. **Run Windows **PowerShell as Administrator and run this:

npm install --global --production windows-build-tools  and wait for it to finish completely, it will take a bit long.

If you are running in some kind of issue:

C:\WINDOWS\System32\WindowsPowerShell\v1.0\

copy and set this as your PATH in your Environment System Variables.

  1. Double check if all is installed, and the most important part is Visual C++ Build Environment. To do so;  download the Visual C++ Build Tools.  You will be prompted to download files, just choose: Build Tools for Visual Studio 2017, it can be 2017 or greater depending when you read this post. But for the time being you will find this:

Capture.PNG

It can be 2017 or greater

  1. Open and Install, you will find this:

Capture.PNG

4.** Don’t worry if it is installed or not**, because what we need to verify is that VC++ is checked; to do so **click on Modify Button.** This will shows up:

Capture.PNG

On the right corner, you will see VC++ 2015V14.00. Just check it! And Click on modify to install it.

  1. Execute npm config set python python2.7 and npm config set msvs_version 2017 on CMD.

  2. Just install this package:

    npm install –save-dev electron-rebuild

Add these two lines under scripts in your **package.json **

carbon(1).png

Figure 0 (because programmers counts from zero, remember ?)

  1. This will simply build native node modules with your app. To do so, write this run:

But make sure you did step 5. It’s important.

npm run rebuild

Afterwards, you can do this below to run your app. It will run Electron flawlessly with your app but it doesn’t end here…

 electron .
  1. Now you will try package your app, and make an executable, but no! Don’t do that yet, you will need to make a file a special**file  in which you will integrate it to your Electron Packager, just use this below and call it build.js if you want and place it at the same directory level of package.json:

If you haven’t installed electron-package:

npm install --save-dev electron-packager

And add this to the file:

const packager = require('electron-packager');
const rebuild = require('electron-rebuild');

packager({
    dir: './',
    overwrite: true,
    asar: true,
    platform: 'win32',
    arch: 'ia32',
    prune: true,
    out: 'release-builds-folder',
    executableName: 'my-app',
    icon: 'icon/myicon.ico',

    afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {

    rebuild.rebuild({ buildPath, electronVersion, arch })

      .then(() => callback())

      .catch((error) => callback(error));

  }],

})

As mentioned in Figure 0, this build.js file needs to be executed to  package your app:

npm run package-rebuild

And now, you have SQLite3 working fine with Electron + you can package your app without an issue!

Image result for relieved meme gif

Now feel relieved, dear brothas and sistas !

If you have questions, please feel free to ask, and don’t forget to share ! Stay tuned on busymind101.wordpress.com

**GITHUB FOR ELECTRON-REBUILD: **https://github.com/electron/electron-rebuild