Electron应用启动报错:找不到pg-hstore模块(SQLite+Sequelize场景)
问题描述
在Electron应用的main.js的createWindow()函数中调用IPC事件,通过Sequelize和Umzug创建SQLite数据库时,应用启动出现以下错误:
编译阶段提示:
"pg-hstore?commonjs-external" 被 "pg-hstore?commonjs-external" 导入,但无法解析——将其视为外部依赖。
✓ 已转换891个模块。
dist-electron/main.js 2,899.97 kB │ gzip: 456.42 kB
构建完成耗时5124ms。
随后运行时抛出异常:
应用加载期间抛出错误
Error: 找不到模块 'pg-hstore'
调用栈:
- /home/chiran/Documents/JEWELLERY PROJECT/Nithyakala_Jewellery_Management_System/application/dist-electron/main.js
- /home/chiran/Documents/JEWELLERY PROJECT/Nithyakala_Jewellery_Management_System/application/node_modules/electron/dist/resources/default_app.asar/main.js
...(剩余调用栈省略)
解决方案
1. 安装pg-hstore依赖
尽管你使用的是SQLite,但Sequelize的依赖树中可能存在对PostgreSQL相关模块的隐式引用,直接安装该模块即可解决找不到的问题:
npm install pg-hstore --save # 或使用yarn yarn add pg-hstore
2. 确认Sequelize的数据库方言配置
确保你的Sequelize实例明确指定dialect为sqlite,避免Sequelize加载PostgreSQL相关依赖:
const { Sequelize } = require('sequelize'); // 正确配置SQLite const sequelize = new Sequelize({ dialect: 'sqlite', storage: './path/to/your/database.sqlite' // 替换为你的SQLite文件路径 });
3. 调整Electron打包配置(针对Vite/Webpack)
如果使用Vite或Webpack打包Electron主进程,需确保pg-hstore被正确打包进主进程代码:
- Vite:在
vite.config.js的electron配置中,将pg-hstore从external列表移除:export default defineConfig({ electron: { build: { external: [] // 不要将pg-hstore标记为外部依赖 } } }); - Webpack:在主进程Webpack配置中,确保
pg-hstore不被设为外部依赖:module.exports = { externals: [ require('webpack-node-externals')({ allowlist: ['pg-hstore'] // 允许打包该模块 }) ] };
4. 清理依赖缓存并重新安装
若以上步骤无效,可能是依赖缓存问题,执行以下命令重置依赖:
# 删除node_modules和锁文件 rm -rf node_modules package-lock.json # 重新安装依赖 npm install
内容的提问来源于stack exchange,提问作者Chiran Jeewantha
相关产品推荐
相关产品推荐

