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
2 Answers
Reset to default 6As 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
版权声明:本文标题:javascript - Electron App : Unable to load preload script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741839356a2400404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论