m4heshd / better-sqlite3-multiple-ciphers

better-sqlite3 with multiple-cipher encryption support 🔒
MIT License
137 stars 27 forks source link

/node_modules/better-sqlite3-multiple-ciphers/build/Release/better_sqlite3.node' was compiled against a different Node.js version using #83

Closed MichaelJCole closed 6 months ago

MichaelJCole commented 6 months ago

Hi, I'm getting a similar issue as #55

Error: The module '/myproject/node_modules/better-sqlite3-multiple-ciphers/build/Release/better_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 119. This version of Node.js requires
NODE_MODULE_VERSION 108. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

better-sqlite3-multiple-ciphers runs in an electron 28.2.2 for my application - which is based on node 18.18.2.
I also have some tests to test my electron code that run in node 18.18.2. Both node and electron are at 108.

The error says the modules were compiled for 119 which seems to be the newest node 20 - which makes me think it's trying to load prebuilt binaries. I don't have node 20 installed.

I've tried npm rebuild, electron-rebuild, and electron-builder install-app-deps, but I get the same error. I tried reinstalling node.

The only way I can get the tests to work is uninstalling and reinstalling better-sqlite3-multiple-ciphers with no package-lock.json

The README says there are prebuild binaries for node LTS releases, but is the node 18 version of the module downloading a prebuilt binary made in node 20?

I don't have node 20 to make such a module it's complaining about. What am I missing? Thank you!

m4heshd commented 6 months ago

It seems like you're trying to run your tests on locally installed Node instead of Electron's embedded Node which has a different module version.

Module version 108 is the Node's module version for Node v18 but that same version of Node embedded in Electron has the module version 119. Electron always reserves its own module versions upon every Node release.

So the solution to your problem is running your tests using Electron's embedded Node runtime. You can do that by setting the ELECTRON_RUN_AS_NODE environment variable to 1.

To make things easier, I suggest using cross-env. Install it as a dev dependency on your project by running the following command.

npm install -D cross-env

Once it's installed, you can add a script to your package.json like the following. Make sure to adjust it to your project structure.

{
    "scripts": {
        "test": "cross-env ELECTRON_RUN_AS_NODE=1 electron test.js"
    }
}

Now, running npm run test would run the tests perfectly since the native modules are already compiled against that version.

MichaelJCole commented 6 months ago

Thank you Mahesh, here's what worked for vitest:

ELECTRON_RUN_AS_NODE=1 electron node_modules/.bin/vitest