毫秒镜像
木雷坞 · MLIEV
See Vite's troubleshooting guide and Rollup's troubleshooting guide for more information too.
If the suggestions here don't work, please try checking the GitHub issue tracker to see if any existing issues match your problem. If you've found a bug, or electron-vite can't meet your needs, please try raising an issue or posting questions on GitHub Discussions.
Through the following steps, you can quickly identify and resolve problems:
debugger statement or breakpoints to locate problems.preview command to simulate the packaged environment and detect potential problems early.--trace-warnings to your app’s launch command to view detailed error messages. For example: .\app.exe --trace-warningsopen app.app --args --trace-warningsdependencies (not devDependencies). Or it may be a pnpm problem (if it is used).The CJS build of Vite's Node API is deprecated Since Vite 5, calling Vite’s CJS Node API will trigger a deprecation warning. electron-vite v2 is now published as an ESM package, so you can update to the latest version.
In addition, make sure that:
electron.vite.config.js file uses ESM syntax.package.json file has "type": "module", or alternatively, rename the config file to use the .mjs extension (e.g. electron.vite.config.mjs).Note that when adding "type": "module" to your project's package.json, if Electron supports ESM (Electron 28+), it will be built as ESM. Read the ESM Support in Electron guide before migrating. But if ESM is not supported, it will be built as CommonJS and all files will have the .cjs extension.
If you prefer not to make any changes and want to continue bundling as CJS, the simplest solution is to rename electron.vite.config.js to electron.vite.config.mjs.
Unable to load preload scripts -> Error: module not found: 'XXX' From Electron 20, preload scripts are sandboxed by default and no longer have access to the full Node.js environment.
You will need to either:
sandbox: false in the BrowserWindow options.require multiple separate files).Uncaught Error: Module "XXX" has been externalized for browser compatibility. or Uncaught ReferenceError: __dirname is not defined You should not use Node.js modules in renderer processes. electron-vite also does not support nodeIntegration.
Error [ERR_REQUIRE_ESM]: require() of ES Module This happens because many libraries (e.g. lowdb, execa, node-fetch) are distributed only as ES modules and therefore cannot be required from CJS code.
There are two ways to resolve this issue:
ESM-only dependencies so they can be used in a CJS environment.import { defineConfig } from 'electron-vite'
export default defineConfig({
main: {
build: {
externalizeDeps: {
exclude: ['foo'] // <- Add related modules to 'exclude' option
}
}
},
// ...
})Related issue: #35
vue-router or react-router-dom works in development but not production Electron does not manage browser history and instead relies on a synchronized URL. Therefore, only a hash-based router will work properly in production.
vue-router, use createWebHashHistory instead of createWebHistory.react-router-dom, use HashRouter instead of BrowserRouter.When using a hash router, you can specify the hash value in the second argument of BrowserWindow.loadFile to load the corresponding page.
win.loadFile(path.join(import.meta.dirname, '../renderer/index.html'), { hash: 'home' })A JavaScript error occurred in the main process -> Error: Cannot find module 'XXX' This error usually occurs because dependent modules are not included in the packaged application. To resolve this issue:
Check dependencies:
If the missing module is listed under devDependencies, reinstall it under dependencies instead. Packaging tools (such as electron-builder or electron-forge) typically exclude modules in devDependencies.
For pnpm users:
You can add a file named .npmrc with shamefully-hoist=true in your project root directory (in order for your dependencies to be bundled correctly). Then delete node_modules and pnpm-lock.yaml, and reinstall your dependencies. Alternatively, you can switch to another package manager (e.g. npm or yarn) to avoid this issue.
A JavaScript error occurred in the main process -> Error: Invaild or incompatible cached data (cachedDataRejected) This issue occurs when build.bytecode is enabled.
To prevent this runtime error, you need to compile the bytecode cache for the target platform and architecture.
See the Limitations of Build section in the Source Code Protection guide for details.