Skip to content

使用 Electron 遇到的坑

Vite and TypeORM

该问题是使用 Electron Forge 中的 Vite + TypeScript 模板时遇到的。

首先我们根据 这篇文章 调整一下 vite.main.config.ts 内容:

ts
import { defineConfig } from 'vite'

// https://vitejs.dev/config
export default defineConfig({
  build: {
    rollupOptions: {
      external: ['typeorm'],
    },
  },
  resolve: {
    // Some libs that can run in both Web and Node.js, such as `axios`, we need to tell Vite to build them in Node.js.
    browserField: false,
    mainFields: ['module', 'jsnext:main', 'jsnext'],
    conditions: ['import', 'module', 'node', 'default'],
  },
})

TIP

VitePlugin 内部有一个插件外部化了 node 的内置模块,所以这里只需外部化 typeorm

在运行 npm start 命令后,会报错:[4192:0907/184556.203:ERROR:crashpad_client_win.cc(844)] not connected。目前的解决办法是把 electron 版本降级到 v25。

References