'UnhandledPromiseRejectionWarning with preload.js (Vue/Electron)
I have installed Vue and run vue add electron-builder to install the Vue CLI Plugin Electron Builder. This generates code for setting up Vue with Electron and I can run the npm run electron:serve command without any issues.
I now want to use the "IPC Without Node Integration" functionality in order to be able to use the ipcMain in background.js and ipcRenderer in Vue. I've followed instructions in the documentation of Vue CLI Plugin Electron Builder and done this:
- Created the
vue.config.jsfile in the root folder and added this code:
module.exports = {
pluginOptions: {
electronBuilder: {
preload: 'src/preload.js'
}
}
}
- Created the
src/preload.jsfile and added the following code:
import { ipcRenderer } from 'electron'
window.ipcRenderer = ipcRenderer
- In the
background.jsfile, I've added one line of code to thecreateWindow()function, so it now looks like this:
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
preload: path.join(__dirname, 'preload.js')
// ...
When running npm run electron:serve, I get an error saying:
(node:91047) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:91047) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The Electron app seems unresponsive and no window is appearing. If I remove the preload: path.join(__dirname, 'preload.js') line in the background.js file, everything works like normal again.
Does anyone know what might be causing this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
