WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.44k stars 395 forks source link

Electron app crashing with latest better-sqlite3 #1062

Closed jayakrishnankp closed 6 months ago

jayakrishnankp commented 1 year ago

I was using better-sqlite3 with the version as ^8.3.0 in package.json in my electron app for some time. On a recent npm install, better-sqlite3 version 8.6.0 was installed, which causes my app to crash with an error message:

node_modules/better-sqlite3/build/Release/better_sqlite3.node' was compiled against a 
different Node.js version using NODE_MODULE_VERSION 108. This version of Node.js 
requires NODE_MODULE_VERSION 116.

NOTE: When I switch my version back to 8.4.0 or less, everything works fine

Steps to Reproduce

  1. Create a sample Electron app (you can clone the quick start project here) or use any existing one
  2. Install better-sqlite3 version 8.6.0 and electron-rebuild
  3. Import better-sqlite3 in main.js and initialize the db:
    const sqlite = require("better-sqlite3");
    const db = sqlite("newdb.sqlite.db")
    console.log("Created db", db); 
  4. Run the electron app using npm run start
  5. See error

Versions

Expected behaviour No error and the console log is expected

Addition Context

neoxpert commented 1 year ago

Did you clean up the node_modules folder? Node Module Version 116 belongs to Electron 25 / 26, not 24.

Due to a shortcoming in how prebuild handles the node module version when resolving package names, this current leads to issues when using Electron 26 with the prebuilt binaries https://github.com/WiseLibs/better-sqlite3/issues/1044#issuecomment-1709549849.

jayakrishnankp commented 1 year ago

Did you clean up the node_modules folder? Node Module Version 116 belongs to Electron 25 / 26, not 24.

Yes. I always remove the node_modules and package-lock before doing npm install.

Due to a shortcoming in how prebuild handles the node module version when resolving package names, this current leads to issues when using Electron 26 with the prebuilt binaries #1044 (comment).

I am not sure if the same happens for older versions as well. I am using Electron 24.1.2 and I couldn't find any issues which mention the error for this version in Electron repo. I am also not using prebuilt binaries in my case. Just using electron-rebuild to rebuild better-sqlite3, then packaging it using electron-forge.

neoxpert commented 1 year ago

Well if you still get the error

node_modules/better-sqlite3/build/Release/better_sqlite3.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 108. This version of Node.js requires NODE_MODULE_VERSION 116.

something on your machine did either resolve the wrong prebuilt binary or you are really executing with Electron 25 / 26. I just downgraded a project to Electron 24.1.2 for testing purposes and had no issues when installing better-sqlite3@8.6.0 aswell as on a fresh project. As I am using electron-builder for the native module part, I tend to suspect electron-rebuild maybe not doing what it should.

jayakrishnankp commented 1 year ago

something on your machine did either resolve the wrong prebuilt binary or you are really executing with Electron 25 / 26. I just downgraded a project to Electron 24.1.2 for testing purposes and had no issues when installing better-sqlite3@8.6.0 aswell as on a fresh project. As I am using electron-builder for the native module part, I tend to suspect electron-rebuild maybe not doing what it should.

Ohhk. Thank you so much for testing it yourself @neoxpert. I'll try the same with electron-builder (hoping it won't require big changes) and see if it's an issue with electron-rebuild.

tetap commented 1 year ago

I lowered it to 25.8.1 with no problem, but I couldn't upgrade to 26.x

TylerTemp commented 11 months ago

I lowered it to 25.8.1 with no problem, but I couldn't upgrade to 26.x

Saved my life

tetap commented 11 months ago

I lowered it to 25.8.1 with no problem, but I couldn't upgrade to 26.x

Saved my life

It is now available to support 26.x 27.x requires 9.0.0

cacieprins commented 10 months ago

I was having a similar issue to @jayakrishnankp , and fixed it by updating my mac to a newer version of OSX.

In my case, the root cause of:

After using electron-rebuild to rebuild the package (which is the usual case), the process exits without any better-sqlite3 error message

was due to a darwin binary being loaded that is not compatible with my current operating system.

I bisected versions down to v8.5.2, which added darwin-arm64 prebuilds. Unfortunately, it seems that prebuild-install does not take into account the version of darwin, and just blindly installs with the assumption that it will work. If you're running an outdated version of osx, loading the darwin prebuilds will crash without an error message.