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 · 185 comments
Open

Help me use Electron! #126

JoshuaWise opened this issue May 4, 2018 · 185 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.

(Edit by @mceachen: electron-rebuild was renamed to @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)

@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 [email protected] with Node v18.15.0, @electron/[email protected], and [email protected]. 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.

@jdeg
Copy link

jdeg commented Jun 18, 2024

Could not dynamically require "better_sqlite3.node" #1102

Did you find the root of this problem? I have a similar issue using Knex. I tried to use nativeBinding but it seems to ignore it.

@Ocean456
Copy link

Ocean456 commented Aug 6, 2024

After I used Electron-builder to package and install the program, an error occurred during the program's operation:

Uncaught Exception:
Error:Could not find module root given file:"node:internal/modules/cjs/loader".Do
you have a package.json'file?

What should I do?

@MikesGlitch
Copy link

MikesGlitch commented Aug 12, 2024

Putting this here in case it helps anyone struggling with electron-builder and better-sqlite3.

My issue was better-sqlite3 was being built by the build machine on x64, even when I was trying to build for arm64. That gave me an x64 better-sqlite3 when I wanted an arm64 one.

The solution was to:

  • hook into the "beforePack" hook
  • manually rebuild better-sqlite3 with @electron/rebuild for the architecture it's trying to pack (with careful reference to the yarn workspace)

To verify the fix I had to unpack the asars on mac, check the node_modules/better_sqlite3 and I compared the file size of the .node file for the x64 and arm64 builds. If the file size is different between arm64 and x64 builds you know it worked.

Code is here: https://github.com/actualbudget/actual/pull/3220/files

@GitMurf
Copy link

GitMurf commented Sep 20, 2024

For the record, I highly recommend using electron forge (which is now officially supported and maintained by core electron team). That along with using Vite, marking better SQLite as external (like most node native addons must do). We have had great success!

@bcdrme
Copy link

bcdrme commented Sep 21, 2024

For the record, I highly recommend using electron forge (which is now officially supported and maintained by core electron team). That along with using Vite, marking better SQLite as external (like most node native addons must do). We have had great success!

Ok wow I did it. Your message put me in the right direction thank you.

My mistake was that I was editing vite.main.config.ts but I really needed to do that in vite.preload.config.ts in order to use sqlite with the contextBridge stuff from electron.
Makes sense now, I feel stupid.

import { defineConfig } from "vite";

// https://vitejs.dev/config
export default defineConfig({
  build: {
    rollupOptions: {
      external: ["better-sqlite3"],
    },
  },
});

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