Skip to content

Native module version mismatch: better-sqlite3 compiled against wrong Node.js version #5

@alexanderkinging

Description

@alexanderkinging

原生模块版本不匹配:better-sqlite3 编译时使用了错误的 Node.js 版本

问题描述

打包后的 CodePilot.app 无法启动,原因是原生模块版本不匹配。better-sqlite3 模块编译时使用的 Node.js 版本与 Electron 内置的 Node.js 版本不一致。

错误信息

Error: The module '/Applications/CodePilot.app/Contents/Resources/standalone/.next/node_modules/better-sqlite3-90e2652d1716b047/build/Release/better_sqlite3.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 127. This version of Node.js requires NODE_MODULE_VERSION 143. Please try re-compiling or re-installing the module (for instance, using npm rebuild or npm install).

at process.func [as dlopen] (node:electron/js2c/node_init:2:2617)
at Module._extensions..node (node:internal/modules/cjs/loader:1929:18)
at Object.func [as .node] (node:electron/js2c/node_init:2:2617)
at Module.load (node:internal/modules/cjs/loader:1488:32)
at Module._load (node:internal/modules/cjs/loader:1307:12)
at c.load (node:electron/js2c/node_init:2:17999)
at TracingChannel.traceSync (node:diagnostics_channel:328:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:252:24)
at Module.require (node:internal/modules/cjs/loader:1511:12)
at mod.require (/Applications/CodePilot.app/Contents/Resources/standalone/node_modules/next/dist/server/require-hook.js:68:28)

运行环境

  • 操作系统: macOS
  • 应用版本: 0.2.2
  • 架构: arm64 (Apple Silicon)

根本原因

better-sqlite3 原生模块编译时使用的是 NODE_MODULE_VERSION 127(Node.js ~22.x),但 Electron 运行时需要的是 NODE_MODULE_VERSION 143

这是因为在打包过程中,原生模块没有针对 Electron 内置的 Node.js 版本重新编译。

建议的修复方案

在打包前使用 electron-rebuild 针对 Electron 的 Node.js 版本重新编译原生模块:

# 安装 electron-rebuild
npm install --save-dev @electron/rebuild

# 在 package.json 中添加脚本
"scripts": {
  "rebuild": "electron-rebuild -f -w better-sqlite3",
  "postinstall": "electron-rebuild -f -w better-sqlite3"
}

或者在 electron-builder.yml 中配置重新编译原生模块:

npmRebuild: true
buildDependenciesFromSource: true

也可以在构建脚本中使用 @electron/rebuild

// scripts/build-electron.mjs
import { rebuild } from '@electron/rebuild';

await rebuild({
  buildPath: process.cwd(),
  electronVersion: '40.x.x', // 匹配你的 Electron 版本
  force: true,
  onlyModules: ['better-sqlite3'],
});

临时解决方案

目前用户可以通过从源码运行来绕过这个问题:

git clone <repo-url>
cd CodePilot
npm install
npm run dev

然后在浏览器访问 http://localhost:3000

参考资料

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions