admin管理员组

文章数量:1310178

Been getting this error every time I start up my electron app.

electron/js2c/renderer_init.js:91 Unable to load preload script: C:\Users\Desktop\Projects\Electron-Apps\Electron\src\preload.js
(anonymous) @ electron/js2c/renderer_init.js:91
electron/js2c/renderer_init.js:91 Error: contextBridge API can only be used when contextIsolation is enabled

My preload.js is in the same directory as my index.js, so I'm a bit loss at debugging this. Not sure why it won't load my preload.js file. Everything was working fine up until I setup my default formatter to prettier with eslint.

Index.js

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

Preload.js

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electron', {
  notificationAPI: {
    sendNotification(message) {
      ipcRenderer.send('notify', message);
    },
  },
  filesAPI: {
    sendEmails(content) {
      ipcRenderer.send('save-delete', content);
    },
  },
});

Been getting this error every time I start up my electron app.

electron/js2c/renderer_init.js:91 Unable to load preload script: C:\Users\Desktop\Projects\Electron-Apps\Electron\src\preload.js
(anonymous) @ electron/js2c/renderer_init.js:91
electron/js2c/renderer_init.js:91 Error: contextBridge API can only be used when contextIsolation is enabled

My preload.js is in the same directory as my index.js, so I'm a bit loss at debugging this. Not sure why it won't load my preload.js file. Everything was working fine up until I setup my default formatter to prettier with eslint.

Index.js

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

Preload.js

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electron', {
  notificationAPI: {
    sendNotification(message) {
      ipcRenderer.send('notify', message);
    },
  },
  filesAPI: {
    sendEmails(content) {
      ipcRenderer.send('save-delete', content);
    },
  },
});
Share Improve this question asked Mar 22, 2021 at 2:04 Chris WongChris Wong 211 gold badge1 silver badge3 bronze badges 1
  • I think, in general this issue is still open (github./electron/forge/issues/2931), I did some tests setting contextIsolation, sandbox, nodeIntegration, enableRemoteModule with all possible values, nothing works. – Alejadro Xalabarder Commented May 28, 2023 at 12:05
Add a ment  | 

2 Answers 2

Reset to default 6

As the error suggests, contextIsolation needs to be enabled to allow you to use the contextBridge API

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true, // add this
      preload: path.join(__dirname, 'preload.js'),
    },
  });

Note: contextIsolation is enabled by default starting from Electron 12.0

I had the similar error. In my case, the cause was there was an importing cycle in the code, i.g. fileA imports fileB imports fileC imports fileA.

本文标签: javascriptElectron AppUnable to load preload scriptStack Overflow