Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help me use Electron! #126

Open
JoshuaWise opened this issue May 4, 2018 · 180 comments
Open

Help me use Electron! #126

JoshuaWise opened this issue May 4, 2018 · 180 comments
Labels

Comments

@JoshuaWise
Copy link
Member

JoshuaWise commented May 4, 2018

Electron Help Thread

This thread is for anyone who needs help using Electron with better-sqlite3.

better-sqlite3 is a Node.js package, not an Electron package. If third parties like Electron decide to do fancy things that happen to break a perfectly functioning Node.js package, it is not an issue with that package. Any issues related to Electron should be reported here. If you're lucky, friendly members of the community may chime in here to help you.

@JoshuaWise
Copy link
Member Author

JoshuaWise commented May 4, 2018

As a potentially helpful start to this thread, I'd like to contribute that many people have had their issues resolved by using electron-rebuild.

@jlongster
Copy link

I'm using both Electron and Windows so I'm happy to chime in here. I'm still using a custom fork but I think you resolved my PRs a while ago so I need to try master again. I'll let you know what we should document about Electron (at the very least, just like any native module, you do need to rebuild it for electron and electron-rebuild works great)

@JoshuaWise
Copy link
Member Author

@jlongster That's great, the help is much appreciated

@mandaputtra
Copy link

I,m still had this error even with electron rebuild

here my package.json

{
  "name": "sqlite-electron",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "rebuild": "electron-rebuild -f -w better-sqlite3",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "better-sqlite3": "^4.1.4",
    "electron": "^2.0.2"
  },
  "devDependencies": {
    "electron-rebuild": "^1.7.3"
  }
}

OS: Linux Ubuntu Based
node 8.11.1 using nvm

@Custardcs
Copy link

Custardcs commented Jul 10, 2018

I can help all our projects are using electron and better-sqlite3

this is our read me

/////////////////////////////////////////////
SETUP AND INSTALL MODULES
/////////////////////////////////////////////
better-sqlite3 instructios

npm install -g windows-build-tools(might need to install python and add the dir to the env file in windows)

if that doesnt work install

python 2.7 (not 3)

then below to add to path.
https://www.pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/

(windows 7 install .net 4.5.1)

go (C:\Users%USERNAME%.windows-build-tools)
run

BuildTools_Full.exe

open BuildTools and click modify

Check VC++ 2015.3 v14.00(v140) toolset for desktop

npm install --save better-sqlite3

npm install --save-dev electron-rebuild

go to terminal in the vs and run

node_modules/.bin/electron-rebuild -f -w better-sqlite3

if you have more than one module that needs building like (bcypt) its best to use

node_modules/.bin/electron-rebuild.

@tracker1
Copy link

It may be best to use nvm and set the same node version to match the version in electron... depending on what/how you are running functional/integration testing, you may still need the version against your framework outside electron.

@LucaPizzagalli
Copy link

Hello,
I want to display some data using a renderer process and accessing a database; the database is open in the main process. I'm trying to pass the database variable from the main to the renderer using ipcRenderer but when I try to call db.prepare() in the renderer i get the error:
"db.prepare is not a function"

Searching throw stackoverflow it seems that the reason is that Electron's IPC only sends serializable object and Database is not (I'm not really sure about this).

How can I pass the database from the main to the renderer?

@SwiTool
Copy link

SwiTool commented Oct 31, 2018

Where it doesn't work for you even with the windows-build-tools, here is the fix :

  • Install Visual Studio Installer
  • Modify Visual Studio Build Tools 2017 (or install if you don't have)
  • Inside Visual C++ Build Tools, tick VC++ 2015.3 v14.00 (v140) toolset
  • Push the Install button (or Modify)
  • Retry running npm install better-sqlite3
  • Run node_modules/.bin/electron-rebuild -f -w better-sqlite3

And there you go, you can run your app.

@JoshuaWise
Copy link
Member Author

JoshuaWise commented Oct 31, 2018

@LucaPizzagalli You can't send database objects across IPC channels, but you can send a filename (string), which you can then use to open the same database within your renderer process.

@chaselal
Copy link

chaselal commented Feb 5, 2019

@JoshuaWise

You can't send database objects across IPC channels, but you can send a filename (string), which you can then use to open the same database within your renderer process.

Would you clarify -- is it true that better-sqlite3 operations called in renderer processes must make remote calls to the main process, and so the main process actually does all database access? Or can renderer processes use better-sqlite3 completely in-process?

@barbalex
Copy link

barbalex commented Feb 7, 2019

In my case node_modules/.bin/electron-rebuild -f -w better-sqlite3 results in this error: https://gist.github.com/barbalex/a40e40900279c0021541e8229d2a25fb

which seems to basically be:

In file included from ../src/addon.cpp:2:
../src/addon.h:21:15: warning: 'MakeCallback' is deprecated: Use MakeCallback(..., async_context) [-Wdeprecated-declarations]
        node::MakeCallback(isolate, isolate->GetCurrentContext()->Global(), Local<Function>::New(isolate, noop), 0, nullptr);

I have the feeling that node versions are involved, but absolutely no idea how to solve this.

It may be related to the fact that when I run yarn dev, electron starts up but this error appears in the dev tools:

/Users/alexandergabriel/awel-personal/app/node_modules/bindings/bindings.js:88 Uncaught Error: The module '/Users/alexandergabriel/awel-personal/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:722)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
    at Module.load (internal/modules/cjs/loader.js:602)
    at tryModuleLoad (internal/modules/cjs/loader.js:541)
    at Function.Module._load (internal/modules/cjs/loader.js:533)
    at Module.require (internal/modules/cjs/loader.js:640)
    at require (internal/modules/cjs/helpers.js:20)
    at bindings (/Users/alexandergabriel/awel-personal/app/node_modules/bindings/bindings.js:81)
    at Object.<anonymous> (/Users/alexandergabriel/awel-personal/app/node_modules/better-sqlite3/lib/database.js:4)

Which is why I tried to run electron-rebuild in the first place.

But no number of rm yarn.lock && rm -rf node_modules && yarn cache clean && yarn has helped.

@Custardcs
Copy link

I had this problem once I built it with the wrong version. Delete your entire Node folder download electron first then better sqlite 3 then try and build. or it could be electron then electron rebuild then sqlite.

@Custardcs
Copy link

Hello,
I want to display some data using a renderer process and accessing a database; the database is open in the main process. I'm trying to pass the database variable from the main to the renderer using ipcRenderer but when I try to call db.prepare() in the renderer i get the error:
"db.prepare is not a function"

Searching throw stackoverflow it seems that the reason is that Electron's IPC only sends serializable object and Database is not (I'm not really sure about this).

How can I pass the database from the main to the renderer?

so what we have done is we set up a worker file.. and the worker file will talk to the DB.. you should never run anything on the main thread.. as it causes a lock up on the clients PC. It took us a while to get this working properly XD. we use our main thread for only page changes and connections to the server. Everything else is run through our worker.

@Escyth
Copy link

Escyth commented Feb 22, 2019

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42

It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.

Any help would be appreciated.

@SwiTool
Copy link

SwiTool commented Feb 22, 2019

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42

It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.

Any help would be appreciated.

You need to install electron version 4.0.3, because since 4.0.4, no node version is high enough to fulfill the 69 version requirement.
as seen here : https://nodejs.org/en/download/releases, the last NODE_MODULE_VERSION is currently 67 for the last version (11.10.0).
If you want to use electron 4.0.3, you'll need to install node v10.15.1

@Custardcs
Copy link

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42
It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.
Any help would be appreciated.

You need to install electron version 4.0.3, because since 4.0.4, no node version is high enough to fulfill the 69 version requirement.
as seen here : https://nodejs.org/en/download/releases, the last NODE_MODULE_VERSION is currently 67 for the last version (11.10.0).
If you want to use electron 4.0.3, you'll need to install node v10.15.1

Im using version 4.0.4 without any issues

@Custardcs
Copy link

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42

It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.

Any help would be appreciated.

can you give me a step by step example of what you have done.. from fresh project to here? so maybe i can help better

@Escyth
Copy link

Escyth commented Feb 22, 2019

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42
It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.
Any help would be appreciated.

can you give me a step by step example of what you have done.. from fresh project to here? so maybe i can help better

I made an application and finished everything, decided to add SQLite to my application and those errors appear when installing/running app/using electron-rebuild.

I'll try to downgrade the version for Electron.

@Custardcs
Copy link

I used the exact same command as barbalex and I received this error: https://gist.github.com/x-Aizawa/cff50adb7f9d4358e35b074a7b89dd42
It seems like the error is different than his/her so I haven't tried the solution provided by Custardcs.
Any help would be appreciated.

can you give me a step by step example of what you have done.. from fresh project to here? so maybe i can help better

I made an application and finished everything, decided to add SQLite to my application and those errors appear when installing/running app/using electron-rebuild.

I'll try to downgrade the version for Electron.

Show me your package.json,

@Custardcs
Copy link

I see it says 'invalid file or disk full' you have enough space on your disk right?

@Escyth
Copy link

Escyth commented Feb 22, 2019

{
  "name": "bank-account",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "uBakkar",
  "devDependencies": {
    "electron": "^4.0.5",
    "electron-rebuild": "^1.8.4"
  },
  "dependencies": {
    "better-sqlite3": "^5.4.0",
    "electron-reload": "^1.4.0",
    "enmap": "^4.8.1",
    "moment": "^2.24.0"
  }
}

Yes, I have 300GB free.

@Escyth
Copy link

Escyth commented Feb 22, 2019

@SwiTool - @Custardcs | I downgraded to v4.0.3 and this error appeared:

Error: The module '\\?\C:\Private\Bank-Application\node_modules\better-sqlite3\build\better_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:722:18)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
    at Module.load (internal/modules/cjs/loader.js:602:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
    at Function.Module._load (internal/modules/cjs/loader.js:533:3)
    at Module.require (internal/modules/cjs/loader.js:640:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Private\Bank-Application\node_modules\better-sqlite3\lib\database.js:5:21)
    at Object.<anonymous> (C:\Private\Bank-Application\node_modules\better-sqlite3\lib\database.js:57:3)

@barbalex
Copy link

I was completely frustrated and just about to give up. So I started checking other sqlite libraries. And found this information: https://github.com/TryGhost/node-sqlite3#custom-builds-and-electron.

So I switched from yarn to npm. Then installed the latest better-sqlite3 (8.0.1) and electron (22.0.1). Then ran this command:

npm install better-sqlite3 --build-from-source --runtime=electron --target=22.0.1 --dist-url=https://electronjs.org/headers --force

And it works 🎆

@barbalex
Copy link

barbalex commented Jan 25, 2023

Meanwhile it seems I have run into the next hurdle: The project now needs to use custom built encrypted databases.

So I am now compiling sqlite using see. Happily I found this advice on how to use such a custom build with better-sqlite3.

And while developing all seemed well and cozy 😀

Then I ran npm run build, hoping to deliver this shiny new piece of software I am a bit proud of. Everything worked and I clicked the .exe to watch it open in all it's glory:

Uncaught Exception: Cannot find module 'better-sqlite3'

Life is hard 😢

I do not really know what is causing this. I think it has to do with the fact that better-sqlite3 is not listed as dependency in package.json. Though due to the preinstall script it is included in the node_modules folder (and thus works in dev mode).

If I add better-sqlite3 to the dependencies in package.json it works. BUT: not the specially compiled, encrypted version is used by the build. Instead, the version listed as dependency. Which can not deal with encrypted db files.

What I have tried is adding

"asarUnpack": [
    "**/node_modules/better-sqlite3/**/*"
  ],

to electron-builder.json. And after building manually copying the better-sqlite3-Folder from node_modules into win-unpacked/resources/app.asar.unpacked/node_modules.

But that does not seem to work.

Any advice? 🙏

Here is my project: https://github.com/barbalex/awel-personal-vite.


Edit: I solved it with two measures:

In electron-builder.json:

"npmRebuild": false

Goal: prevent electron-builder from rebuilding the native module itself.

After running the prebuild-script, add this line to dependencies inside package.json:

"better-sqlite3": "file:./node_modules/better-sqlite3",

Goal: Give electron-builder a module since it seems to want one.

Life is great again 😄

@LudwigWS
Copy link

This is how I solved the trouble, struggled for a long long time... There is a point that other issues don't mention, that is if the better-sqlite is installed with --save-dev, execute electron-rebuild command should add -t dev parameter, otherwise better-sqlite3 won't be rebuilt.

  • Problem log
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 113.
  • (Worth noting but doesn't seem necessary: Use the same Node.js version of Electron was built on.)
npm install --save-dev electron-rebuild
npm install --save-dev better-sqlite3

npx electron-rebuild -f -w -t dev better-sqlite3

@QThans
Copy link

QThans commented Jun 6, 2023

Meanwhile it seems I have run into the next hurdle: The project now needs to use custom built encrypted databases.

So I am now compiling sqlite using see. Happily I found this advice on how to use such a custom build with better-sqlite3.

And while developing all seemed well and cozy 😀

Then I ran npm run build, hoping to deliver this shiny new piece of software I am a bit proud of. Everything worked and I clicked the .exe to watch it open in all it's glory:

Uncaught Exception: Cannot find module 'better-sqlite3'

Life is hard 😢

I do not really know what is causing this. I think it has to do with the fact that better-sqlite3 is not listed as dependency in package.json. Though due to the preinstall script it is included in the node_modules folder (and thus works in dev mode).

If I add better-sqlite3 to the dependencies in package.json it works. BUT: not the specially compiled, encrypted version is used by the build. Instead, the version listed as dependency. Which can not deal with encrypted db files.

What I have tried is adding

"asarUnpack": [
    "**/node_modules/better-sqlite3/**/*"
  ],

to electron-builder.json. And after building manually copying the better-sqlite3-Folder from node_modules into win-unpacked/resources/app.asar.unpacked/node_modules.

But that does not seem to work.

Any advice? 🙏

Here is my project: https://github.com/barbalex/awel-personal-vite.

Edit: I solved it with two measures:

In electron-builder.json:

"npmRebuild": false

Goal: prevent electron-builder from rebuilding the native module itself.

After running the prebuild-script, add this line to dependencies inside package.json:

"better-sqlite3": "file:./node_modules/better-sqlite3",

Goal: Give electron-builder a module since it seems to want one.

Life is great again 😄

I solved my problem with this advice, let my life be great again😄😄
However,

"better-sqlite3": "file:./node_modules/better-sqlite3",

for me, I used

"better-sqlite3": "file:./node_modules/better-sqlite3/bin",

@babyxiaojine
Copy link

我的问题情况如下:

  1. 使用electron19+better-sqlite3,然后在执行electron .的时候,报错nodegyp错误,后来找到解决办法,先安装electron-rebuild,然后执行以下命令,并加入到package.json文件的scripts中
    electron-rebuild -f -w better-sqlite3
    执行之后可以顺利执行electron .
  2. 然而在electron-builder时,却出现问题,后来又发现了electron-rebuild还有另外一个命令,
    electron-rebuild -f -w -t dev better-sqlite3
    执行之后可以顺利 electron-builder

@bjornhanson
Copy link

I'm still unable to upgrade to anything beyond Electron 19 because electron-rebuild is failing to rebuild better-sqlite3. I'm seeing the same errors @ilDon mentioned a year ago (#126 (comment)). I recently looked at this again and tried all of the current stable versions of Electron while running the same version of Node they use. Currently trying electron@25.5.0 with Node v18.15.0, @electron/rebuild@3.2.13, and better-sqlite3@8.5.0. Here are some error examples:

In file included from ../src/better_sqlite3.cpp:4:
In file included from ./src/better_sqlite3.lzz:11:
In file included from /Users/bjorn/.electron-gyp/25.5.0/include/node/node.h:79:
In file included from /Users/bjorn/.electron-gyp/25.5.0/include/node/v8.h:24:
In file included from /Users/bjorn/.electron-gyp/25.5.0/include/node/v8-array-buffer.h:13:
In file included from /Users/bjorn/.electron-gyp/25.5.0/include/node/v8-object.h:9:
/Users/bjorn/.electron-gyp/25.5.0/include/node/v8-maybe.h:106:69: error: expected '(' for function-style cast or type construction
        template <class U, std::enable_if_t<!std::is_lvalue_reference_v<U>>*>
                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

./src/util/macros.lzz:157:21: error: no member named 'AccessorSignature' in namespace 'v8'
                v8::AccessorSignature::New(isolate, rcv)
                ~~~~^

../src/util/binder.lzz:37:51: error: no member named 'CreationContext' in 'v8::Object'
                 v8::Local<v8::Context> ctx = obj->CreationContext();
                                              ~~~~~^

I've seen some related things around GitHub, but haven't found anything that works. One person thinks it's a bug in Node that needs to get resolved. I'm not sure where to go from here, other than to sit on Electron 19, which isn't supported anymore. Any ideas? Thanks in advance!

@mceachen
Copy link
Member

Know that there are a ton of caches that may need to be blown away before you get an actual "clean" build. I've got this in my clean:gyp task:

rm -rf ~/.electron/ ~/.cache/electron* ~/.electron-gyp/ ~/.node-gyp/ ~/.cache/yarn/

@VishwaJay
Copy link

So, on a vanilla electron install without modification, I ran into this issue.

I installed better-sqlite3 using npm install -D better-sqlite3 and it installed.

Ran electron, got the error as described.

Added this to package.json as a script:

"rebuildsqlite": "npx electron-rebuild -f -w -t dev better-sqlite3"

Remember to add commas as appropriate to delimit the script lines.

Error disappeared. Added a quick reference to a sqlite database and tested a quick write-read entry. Fully working!

@quantuminformation
Copy link

i get this error even with not using electron anywhere

@humanityjs
Copy link

Hi. I'm having issues getting better-sqlite3 to work with my electron app. The installation was successful but keep getting this error on start.

App threw an error during load
Error: dlopen(/Users/danielbamidele/csuite/release/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node, 0x0001): tried: '/Users/danielbamidele/csuite/release/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/danielbamidele/csuite/release/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node' (no such file), '/Users/danielbamidele/csuite/release/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))
    at process.func [as dlopen] (node:electron/js2c/asar_bundle:2:1869)
    at Object.<anonymous> (node:internal/modules/cjs/loader:1356:18)
    at Object.func [as .node] (node:electron/js2c/asar_bundle:2:1869)
    at Module.load (node:internal/modules/cjs/loader:1126:32)
    at node:internal/modules/cjs/loader:967:12
    at Function.<anonymous> (node:electron/js2c/asar_bundle:2:13377)
    at Function.Module._load (/Users/danielbamidele/csuite/node_modules/runtime-required/runtime-required.js:34:23)
    at Module.require (node:internal/modules/cjs/loader:1150:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at bindings (/Users/danielbamidele/csuite/release/app/node_modules/bindings/bindings.js:112:48)

I have tried quite a few suggestions to fix this including running DEBUG=electron-builder ./node_modules/.bin/electron-builder build --mac --arm64 postinstall. Nothing seems to be working.

I'll really appreciate any help here. Thanks.

@agamm
Copy link

agamm commented Nov 11, 2023

I'm trying to import better-sqlite3 but I have a problem when I try to use it with an electron app.

When I add this:
import Database from "better-sqlite3";

I get:

App threw an error during load
Error: Could not dynamically require "C:\Users\myuser\windev\project\build\better_sqlite3.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
    at ne (C:\Users\myuser\windev\project\.vite\build\main.js:1:526)
    at h (C:\Users\myuser\windev\project\.vite\build\main.js:1:3441)
    at new m (C:\Users\myuser\windev\project\.vite\build\main.js:3:12762)
    at Object.<anonymous> (C:\Users\myuser\windev\project\.vite\build\main.js:3:13759)
    at Module._compile (node:internal/modules/cjs/loader:1271:14)
    at Object..js (node:internal/modules/cjs/loader:1326:10)
    at Module.load (node:internal/modules/cjs/loader:1126:32)
    at node:internal/modules/cjs/loader:967:12
    at Function._load (node:electron/js2c/asar_bundle:2:13327)
    at loadApplicationPackage (C:\Users\myuser\windev\project\node_modules\electron\dist\resources\default_app.asar\main.js:121:16)

Using:

  • Node v20.9.0
  • Windows 11
  • electron (27.0.4) forge (6.4.2) with the vite plugin.

I tried several answers here without making it work.
Anything I'm missing?

@crsergio
Copy link

I'm trying to import better-sqlite3 but I have a problem when I try to use it with an electron app.

When I add this: import Database from "better-sqlite3";

I get:

App threw an error during load
Error: Could not dynamically require "C:\Users\myuser\windev\project\build\better_sqlite3.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
    at ne (C:\Users\myuser\windev\project\.vite\build\main.js:1:526)
    at h (C:\Users\myuser\windev\project\.vite\build\main.js:1:3441)
    at new m (C:\Users\myuser\windev\project\.vite\build\main.js:3:12762)
    at Object.<anonymous> (C:\Users\myuser\windev\project\.vite\build\main.js:3:13759)
    at Module._compile (node:internal/modules/cjs/loader:1271:14)
    at Object..js (node:internal/modules/cjs/loader:1326:10)
    at Module.load (node:internal/modules/cjs/loader:1126:32)
    at node:internal/modules/cjs/loader:967:12
    at Function._load (node:electron/js2c/asar_bundle:2:13327)
    at loadApplicationPackage (C:\Users\myuser\windev\project\node_modules\electron\dist\resources\default_app.asar\main.js:121:16)

Using:

  • Node v20.9.0
  • Windows 11
  • electron (27.0.4) forge (6.4.2) with the vite plugin.

I tried several answers here without making it work. Anything I'm missing?

I had the same issue. I fixed it by externalizing 'better-sqlite3' in Vite's config:

import { defineConfig } from "vite";
import builtInModules from "builtin-modules";

export default defineConfig({
	build: {
		// ...
		rollupOptions: {
			external: [...builtInModules, "electron", "better-sqlite3"],
			// ...
		},
	},
});

@guspuffygit
Copy link

Am I stupid? I can't find a node version that corresponds with NODE_MODULE_VERSION 113, as far as I can tell node 19 is 111 and node 20 is 115. electron-rebuild permutations are not working for me. This is using latest 9.1.1

 The module '/home/runner/work/sentient-sims-app/sentient-sims-app/release/app/node_modules/better-sqlite3/build/Release/better_sqlite3.node'
    was compiled against a different Node.js version using
    NODE_MODULE_VERSION 113. 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`).

@m4heshd
Copy link
Contributor

m4heshd commented Nov 30, 2023

@guspuffygit I'm not sure what exactly your goal is because according to that error, it's compiled for Electron v23 (NODE_MODULE_VERSION 113) but it's trying to run on Node v18 (NODE_MODULE_VERSION 108). electron-rebuild seem to be doing its job if you're using Electron v23. If you want to run the application on plain Node v18, run npm rebuild.

Check these references:
https://nodejs.org/en/about/previous-releases
https://releases.electronjs.org/releases.json

@guspuffygit
Copy link

guspuffygit commented Nov 30, 2023

Thank you @m4heshd I figured it out.

It was compiling correctly with the correct electron binaries. My issue was when running the jest test it was running jest with node (108) instead of electron which is where the error was happening. I was able to fix my issue by running jest using electron:

ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/.bin/jest

@JijaProGamer
Copy link

I have this error:

#1111

How could I fix it?

@impactcolor
Copy link

Does anyone have any instructions on how to install better-sqlite3 on Electron
electron: 26.2.1
running on a mac with node 16.
I've tried all of the above and keep getting errors.

@impactcolor
Copy link

Answering my own question I found this very helpful in SO:
https://stackoverflow.com/a/69960505/2690257
yarn add better-sqlite3; cd node_modules/better-sqlite3; ../.bin/electron-rebuild cd ../.. npm run start

@flaviut
Copy link

flaviut commented Jan 11, 2024

I'm using esbuild, but not using electron. The solution for me was to add the following loader:

/**
 * rewrites code like `require('bindings')('better_sqlite3.node')`
 * in dependencies to `require('<package_name>/build/Release/better_sqlite3.node')` so that it is
 * picked up by esbuild and correctly found at runtime.
 *
 * Without this, we have two problems:
 *
 * 1. the native addon is not included in the bundle
 * 2. we get this error at runtime: "Could not find module root given file: \"/var/task/index.js\". Do you have a `package.json` file?"
 */
class BindingsRewritePlugin {
  name = 'bindings-rewrite-plugin'

  /** @param {esbuild.PluginBuild} build */
  setup(build) {
    const BINDINGS_PATTERN = /require\(["']bindings["']\)\('([\w-]+.node)'\)/
    build.onLoad({ filter: /\.js$/ }, async args => {
      const contents = await fs.readFile(args.path, 'utf8')
      if (!BINDINGS_PATTERN.test(contents)) return

      // path is an absolute path to the file, so we need to first find the node_modules directory
      const pathParts = args.path.split(path.sep)
      const nodeModulesIndex = pathParts.indexOf('node_modules')
      const packageName = pathParts[nodeModulesIndex + 1]

      const rewritten = contents.replace(
        BINDINGS_PATTERN,
        `require('${packageName}/build/Release/$1')`
      )
      return { contents: rewritten, loader: 'js' }
    })
  }
}

and the following to the esbuild config:

{
    ...
    plugins: [new BindingsRewritePlugin()],
    loader: {
      '.node': 'copy',
    },
  }

the bindings package is literally just a hardcoded list of different requires to try, and these days, in practice, it seems like it is sufficient to only look in build/Release/foo.node

@Trinovantes
Copy link

I was constantly getting TypeError: Cannot read properties of null (reading 'indexOf') from bindings package while using webpack5. The bindings package is just throwing a dummy exception and parsing the stack trace but for some reason the stack trace for me was:

[
  CallSite {}, CallSite {},
  CallSite {}, CallSite {},
  CallSite {}, CallSite {},
  CallSite {}, CallSite {},
  CallSite {}, CallSite {}
]

And thus breaking bindings.getFileName(). Considering bindings package hasn't been updated in 5 years, I doubt this issue will ever get resolved.

Instead I decided to just manually load the bindings:

    import BetterSqlite3 from 'better-sqlite3'

    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const nativeBinding = require('better-sqlite3/build/Release/better_sqlite3.node') as string // technically an object but the nativeBinding field is typed as string | undefined

    const client = new BetterSqlite3(':memory:', {
        nativeBinding,
    })

This also requires node-loader plugin for webpack

    module: {
        rules: [
            {
                test: /\.node$/,
                loader: 'node-loader',
            },
        ],
    },

@bitsmanent
Copy link

For anyone bundling with esbuild just add --external:better-sqlite3. It worked for me, here's the whole command:

esbuild foo.js --outdir=bar --bundle --platform=node --external:better-sqlite3

Hope this helps.

@deadcoder0904
Copy link

If you use Docker, then check out my solution.

I solved it by installing better-sqlite3 in Dockerfile instead of package.json & using pnpm's supportedArchitectures feature.

@n4mt4r
Copy link

n4mt4r commented Mar 18, 2024

Hello together,

I'm new to Electron and vite and would like to get it running with sqlite3 or better-sqlite3. As I had many issues with sqlite3 due to either not compatible with vite in one version but also not compatible with eletron-builder with newer version I decided to use better-sqlite3 which basically solves some of my issues with vite and electron-builder

Now I'm stucked at the point:

Error: Could not dynamically require "C:\workspace\electron-typescript-template_two\build\better_sqlite3.node".

Hope someone here could help me get this running.

This is my package.json

{
  "name": "electron-typescript-template",
  "version": "0.0.1",
  "description": "This is an electron typescript template",
  "private": true,
  "main": "./dist-electron/main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc && vite build",
    "start": "tsc && vite",
    "dist": "electron-builder"
  },
  "author": "David",
  "license": "UNLICENSED",
  "devDependencies": {
    "builtin-modules": "^3.3.0",
    "electron": "^29.1.0",
    "electron-builder": "^22.2.0",
    "typescript": "^5.3.3",
    "vite": "^5.1.4",
    "vite-plugin-electron": "^0.28.2",
    "vite-plugin-electron-renderer": "^0.14.5"
  },
  "build": {
    "appId": "com.wanner.electron-typescript-template",
    "files": [
      "dist-electron",
      "dist"
    ],
    "mac": {
      "category": "your.app.category.type"
    },
    "directories": {
      "output": "build/Release/${version}"
    }
  },
  "debug": {
    "env": {
      "VITE_DEV_SERVER_URL": "http://127.0.0.1:7777"
    }
  },
  "dependencies": {
    "@types/jquery": "^3.5.29",
    "better-sqlite3": "^9.4.3",
    "jquery": "^3.7.1"
  }
}

this is my vite.config.ts

import { defineConfig } from 'vite';
import electron from 'vite-plugin-electron/simple'
import builtInModules from 'builtin-modules';

export default defineConfig({
  plugins: [
    electron({
      main: {
        // Shortcut of `build.lib.entry`
        entry: 'app/electron/main.ts',
      },
      preload: {
        // Shortcut of `build.rollupOptions.input`
        input: 'app/electron/preload.ts',
      },
      // Optional: Use Node.js API in the Renderer process
      renderer: {},
    }),
  ],
});

adding the following lines to vite.config.ts doesn't solve my issue

build: {
   rollupOptions: {
      external: [...builtInModules, "electron", "better-sqlite3"],
   },
}

For electron-builder I'm getting following issues:

prebuild-install http 404 https://github.com/WiseLibs/better-sqlite3/releases/download/v9.4.3/better-sqlite3-v9.4.3-electron-v121-win32-x64.tar.gz
prebuild-install warn install No prebuilt binaries found (target=29.1.0 runtime=electron arch=x64 libc= platform=win32)

@Jazcash
Copy link

Jazcash commented Apr 8, 2024

Am using node 18.18.0, electron ^29.0.0, electron-builder ^24.13.3, vite ^5.2.8, better-sqlite3 ^9.4.5. I'm using electron-builder install-app-deps which fetches the prebuilt bindings successfully.

I've had this issue in the past but can't remember how I fixed it. For some reason electron seems to be searching in the root of the project for the bindings, but the actual binding exists in node_modules/better-sqlite3/build/Release/better_sqlite3.node, so I'm wondering why bindings.js isn't looking in there.

Uncaught (in promise) Error: Could not locate the bindings file. Tried:
 → C:\Users\Jaz\Repos\bar-lobby\build\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\build\Debug\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\build\Release\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\out\Debug\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\Debug\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\out\Release\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\Release\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\build\default\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\compiled\20.9.0\win32\x64\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\addon-build\release\install-root\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\addon-build\debug\install-root\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\addon-build\default\install-root\better_sqlite3.node
 → C:\Users\Jaz\Repos\bar-lobby\lib\binding\node-v121-win32-x64\better_sqlite3.node
    at bindings (bindings.js:126:9)
    at new Database (database.js:48:45)
    at new CacheDbAPI (cache-db.ts:32:27)

Have tried deleting node_modules and package-lock. Tried to configure the nativeBinding property on the Database instatiation but I'm not sure how to do that using import.meta syntax (which my project needs instead of require).

Copying the binding manually to the root folder does fix the issue, but it feels hacky and I'll need to do it dynamically for contributors using different architectures. Am more curious about the exact cause of the issue.

Also, as a side note, this mega-thread is pretty awful for finding answers relevant to my specific problem. There are other issues which are more specific but they got closed in favour of this one, but it's full of tons of people with different problems and a bunch of them are hidden so it's difficult to find relevant solutions. I'd recommend allowing each specific error to exist as its own issue.

@Shaaa-2
Copy link

Shaaa-2 commented May 1, 2024

I had no luck by directly running electron-rebuild. I had to change some build configurations.

Inside package.json,

build {
    "npmRebuild": true,
    "buildDependenciesFromSource": true,
}

Now, run electron-rebuild. This will build better-sqlite3 from source for your electron and node versions. Build will take some time. So, second time change the configuration to :

build {
    "npmRebuild": false,
    "buildDependenciesFromSource": false,
}

This configuration will not build from source or download pre-built binary of better-sqlite3. It will take the binary from the first run of electron-rebuild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests