如何在Electron Forge+Sequelize生产环境中正确集成SQLite3?
解决Electron Forge打包后SQLite3 + Sequelize无法加载的问题
问题描述
开发基于Electron Forge、集成SQLite3与Sequelize的应用时,开发阶段运行正常,但打包分发后,Windows和macOS启动可执行文件均出现以下错误:
A JavaScript error occurred in the main process Uncaught Exception: Error: Please install sqlite package manually at z._loadDialectModule (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1038558) at new z (Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1122797) at new b (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/webpack/main/index.js:2:1584623) at new P (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1160959) at 9678 (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1696966) at o (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1729848) at 4934 (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1281401) at o (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1729848) at 629 (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/webpack/main/index.js:2:132916) at o (/Users/evilscaught/Documents/e-tabulator/out/e-tabulator-darwin-arm64/e-tabulator.app/Contents/Resources/app.asar/.webpack/main/index.js:2:1729848)
解决方案
1. 重建SQLite3适配Electron版本
SQLite3是原生模块,需针对当前Electron版本重新编译:
- 安装依赖:
npm install sqlite3 --save - 执行重建命令:
npx electron-rebuild -f -w sqlite3 - 可在
package.json中添加脚本简化操作:
之后运行"scripts": { "rebuild": "electron-rebuild -f -w sqlite3" }npm run rebuild即可。
2. 配置Webpack排除原生模块
Electron Forge的Webpack插件默认会打包所有模块,但原生模块不能被webpack编译,需在主进程webpack配置中排除:
- 在
forge.config.js中指定主进程webpack配置文件:module.exports = { plugins: [ new WebpackPlugin({ mainConfig: './webpack.main.config.js', // 其他配置... }) ] }; - 在
webpack.main.config.js中添加externals:module.exports = { // 其他配置... externals: { sqlite3: 'commonjs sqlite3' } };
3. 手动指定Sequelize的SQLite模块
避免Sequelize动态加载模块失败,初始化时直接传入sqlite3模块:
const sqlite3 = require('sqlite3'); const { Sequelize } = require('sequelize'); const sequelize = new Sequelize({ dialect: 'sqlite', storage: './database.sqlite', // 打包后建议使用app.getPath('userData')指定存储路径 dialectModule: sqlite3 });
4. 配置Asar包提取原生模块
原生二进制文件无法在asar包内直接运行,需配置Electron Forge将其提取到外部:
在forge.config.js的packagerConfig中添加:
packagerConfig: { asar: true, asarUnpack: ['node_modules/sqlite3/**/*'] }
内容的提问来源于stack exchange,提问作者Scott Mckay
相关产品推荐
相关产品推荐

