electron安装使用better-sqlite3并解决NODE_MODULE_VERSION xxx. This version of Node.js requires

Song149 次浏览0个评论2024年11月06日

一、安装 electron-rebuild 和 better-sqlite3

npm install electron-rebuild
npm install better-sqlite3

Electron 内置的 Node.js 版本和编译到 better-sqlite3 的 Node.js 版本不同将可能导致安装失败,所以此处需要安装 electron-rebuild 重建 Node.js 模块。(注意安装顺序,否则可能出现安装失败的问题)

二、使用 electron-rebuild 重建 Node.js 模块

如果安装完 better-sqlite3 不重建直接运行,则可能出现以下报错:

Error: The module 'xxx'
was compiled against a different Node.js version using
NODE_MODULE_VERSION xxx. This version of Node.js requires
NODE_MODULE_VERSION xxx. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

修改 package.json 文件中的 scripts 内容:

  "scripts": {
    "dev": "vite",
    ...
    "rebuild": "electron-rebuild -f -w better-sqlite3"
  },

执行命令下方命令重建 Node.js 版本:

npm run rebuild


更多相关好文