drizzle-team / drizzle-team/drizzle-orm

[BUG]: Making drizzle-kit Work with Electron

Open
#3,221 5 comments 2 reactions 0 assignees View on GitHub
bug drizzle/kit
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

?

### What version of `drizzle-kit` are you using?

?

### Describe the Bug

From drizzle-kit-mirror repo: https://github.com/drizzle-team/drizzle-kit-mirror/issues/488.

> ## What Happened
>
> When using the drizzle-kit CLI, such as `npx drizzle-kit push`, in an Electron project with better-sqlite3, developers may encounter an error that complains about a Node.js version mismatch.
>
> ```
> Error: The module '/Users/yunhao/Developer/MyApp/node_modules/better-sqlite3/build/Release/better_sqlite3.node'
> was compiled against a different Node.js version using
> NODE_MODULE_VERSION 123. This version of Node.js requires
> NODE_MODULE_VERSION 115. Please try re-compiling or re-installing
> the module (for instance, using `npm rebuild` or `npm install`).
> ```
>
> Note
>
> I will explain the reason and provide a solution. **However, the solution requires editing drizzle-kit's code base.**
> ## The Reason
>
> This issue arises because there are two different Node.js runtimes when developing an Electron app:
>
> * Node.js bundled with Electron
>
> * Node.js installed on your system (macOS/Linux/Windows)
>
>
> These two Node.js runtimes typically have different versions and ABIs. It's often not possible to make them the same because Electron uses a specially modified Node.js.
>
> See: https://www.electronjs.org/docs/latest/tutorial/using-native-node-modules
>
> When the Electron app is running, it uses the bundled Node.js to run better-sqlite3. However, drizzle-kit uses your system's Node.js. Because there are two different Node.js runtimes, if better-sqlite3 works well in Electron, it will likely break when used in your system's command line with drizzle-kit.
> ## The Solution
>
> The solution is simple. We just need to use the same Node.js runtime to run the Electron app and the drizzle-kit CLI. Fortunately, Electron provides an environment variable `ELECTRON_RUN_AS_NODE` which allows us to start Electron as a normal Node.js process.
>
> First, create a file `bin/node` in your project root with the following content:
>
> ```shell
> #!/bin/sh
> SCRIPT_DIR=$(dirname "$(realpath "$0")")
> ELECTRON_RUN_AS_NODE=true "$SCRIPT_DIR/../node_modules/electron/dist/Electron.app/Contents/MacOS/Electron" "$@"
> ```
>
> Grant execution permission to it:
>
> ```shell
> chmod +x ./bin/node
> ```
>
> Then, we need to modify `node_modules/drizzle-kit/bin.cjs` to support environment variable `ELECTRON_RUN_AS_NODE `.
> image
>
> Finally, we can run `PATH=./bin:$PATH npx drizzle-kit push` and everyting works fine.
>
> ```shell
> > PATH=./bin:$PATH npx drizzle-kit push
> drizzle-kit: v0.22.7
> drizzle-orm: v0.31.2
>
> No config path provided, using default path
> Reading config file '/Users/yunhao/Developer/MyApp/drizzle.config.ts'
> [✓] Pulling schema from database...
>
> [i] No changes detected
> ```
>
> ## What's next
>
> Because drizzle-kit is not open source, I cannot make a pull request. The file `node_modules/drizzle-kit/bin.cjs` needs to be modified as described above to make drizzle-kit work with an Electron project.

### Expected behavior

_No response_

### Environment & setup

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.