You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron Forge打包含sqlite3的Electron应用报错,添加externals配置无效求助

解决Electron Forge + Webpack + SQLite3打包运行报错问题

以下是针对问题的具体解决步骤:

1. 修正Webpack主进程配置

在webpack.main.config.ts中,将externals配置改为指定模块类型的写法(数组形式无法正确识别原生commonjs模块):

export default {
  // 保留原有其他配置
  externals: {
    sqlite3: 'commonjs sqlite3'
  },
  target: 'electron-main' // 确保此配置存在,主进程Webpack必须指定该target
};

该配置会让Webpack跳过打包sqlite3模块,保留原生模块的引用逻辑。

2. 重新编译SQLite3适配Electron版本

SQLite3是原生模块,需要针对当前Electron版本重新编译,执行命令:

npx electron-rebuild -f -w sqlite3

也可以在package.json中添加postinstall脚本,确保每次安装依赖后自动执行编译:

{
  "scripts": {
    // 保留原有其他脚本
    "postinstall": "electron-rebuild"
  }
}

3. 补充Electron Forge打包配置

在forge.config.ts中添加额外资源配置,确保SQLite3的二进制文件被复制到打包后的应用目录:

import type { ForgeConfig } from '@electron-forge/shared-types';
import { WebpackPlugin } from '@electron-forge/plugin-webpack';
import mainConfig from './webpack.main.config';
import rendererConfig from './webpack.renderer.config';

const config: ForgeConfig = {
  packagerConfig: {
    extraResource: ['./node_modules/sqlite3/lib/binding/']
  },
  plugins: [
    new WebpackPlugin({
      mainConfig,
      renderer: {
        config: rendererConfig,
        entryPoints: [
          {
            html: './src/index.html',
            js: './src/renderer.ts',
            name: 'main_window'
          }
        ]
      }
    })
  ]
};

export default config;

4. 规范模块使用逻辑

渲染进程无法直接调用原生模块,所有数据库操作逻辑需放在主进程中,通过Electron的IPC机制与渲染进程通信。


内容的提问来源于stack exchange,提问作者Kain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 14:22:40