Integrating Sqlite3 in electron project built with Electron-forge

I’ve been trying to integrate sqlite3 into my electron project which is built using electron-forge.

This is a brief and to the point article on how I solved this problem.

Here is my app’s tech stack:

  1. Electron.
  2. React.
  3. Electron-forge.
  4. Webpack (electron-forge).
  5. TypeScript.

NOTE: After the first few attempt and some research, I realized that better-sqlite3 was an advance package over sqlite3 hence I tried solving things for better-sqlite3 .

Following things I tried and failed:

  1. re-building the sqlite3 module with node-gyp
  2. using better-sqlite3 instead of sqlite3
  3. Working with windows-build-tool as suggested in this article .

The only thing that worked eventually was making better-sqlite3 as externals in webpack config.

With this, I was able to run the app and create a database.

But when I tried packaging the app, better-sqlite3 was missing from the package. This is because we have added this package as an external dependency.

Was hopeless and it was when I decided to use other packaging tools than electron-forge

While checking the old issue pages, I saw that better-sqlite3 have updated their package and is no more dependent on the native modules.

better-sqlite 7.0.1

It means that we would be able to make use of better-sqlite like any other module.

Now all I had to do was:

yarn add better-sqlite3
yarn add @types/better-sqlite3 -D

This way, I was able to integrate better-sqlite3 on both dev env as well as on the packaged app.

I hope you found this article useful. I would love to hear your thoughts. 😇

Thanks for reading. 😊

Cheers! 😃

If you find this article useful, you can show your appreciation by clicking on the clap button. As the saying goes, ‘When we give cheerfully and accept gratefully, everyone is blessed’.

I write articles on JavaScript, electron, and react. Opted for curators for better reach, not money. Open link in incognito to read for free (or contact me)