Electron中的nodeIntegration不起作用,还有一些奇怪的Electron行为

6

我是 Electron 的新手,我一直在努力使它工作。但我遇到了一些无法解释的问题,以下是描述:

即使我已将 nodeIntegration 设置为 true,在网页内部仍然出现了 "Uncaught ReferenceError: require is not defined" 错误。

File Tree:
./
index.html
index.js
package-lock.json
package.json
node_modules/

index.js:

const electron = require("electron");
const Ffmpeg = require("fluent-ffmpeg");
const CmdExec = require('child_process');
const {
    app,
    BrowserWindow,
    ipcMain
} = electron;

function createWindow() {
//If I put the main window ini into here, and then call app.on("ready", createWindow()); app says
//"Cant create window before ready", even though I just moved the funcion from inside ready to here..
}

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
    mainWindow.loadURL(`${__dirname}/index.html`);
});
ipcMain.on("video:submit", (event, path) =>{
    CmdExec.exec("echo hello", (value)=>{console.log(value)});
});

html:

<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
</head>

<body>
    <h1>WELCOME!</h1>
    <script src="" async defer></script>
    <form action="">
        <div>
            <br>
            <label for=""></label>
            <input type="file" accept="video/*" name="" id="">
        </div>
        <button type="submit">get info</button>
    </form>

    <script>
        const electron = require("electron");

        electron.send('perform-action', args);

        document.querySelector("form").addEventListener("submit", (event) => {
            event.preventDefault();
            const {path} = document.querySelector("input").files[0];
            window.api.send("video:submit", path);
        });
            //Tried multiple methos Ive found on stackoverflow,, dont think I implemented them right 
            //though
    </script>
</body>

</html>

package.json:

{
  "name": "media_encoder",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "electron": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^12.0.0"
  }
}
1个回答

13

Electron 12现在将contextIsolation默认设置为true,这会禁用Node(这里是发布说明;这里是PR)。

这里有关于此更改的讨论。值得一提的是,nodeIntegration将在未来的Electron版本中被移除。

最简单的解决方法是简单地禁用上下文隔离:

mainWindow = new BrowserWindow({
      webPreferences: {
          nodeIntegration: true,
          contextIsolation: false
     }
});

话虽如此,出于安全考虑,您可能希望保持启用contextIsolation。请参见此文档,了解此功能如何增强应用程序的安全性。


谢谢,我在stackoverflow上搜索了这个问题但是没有找到答案。这个解决了我的问题,但是如果你不推荐这种方法,你会如何实现网站和我的应用程序之间的通信呢?我对node JS很陌生,所以我大致理解app.on(),但是在文档或其他地方都找不到其他解决方案。 - djkato
2
@djkato 这不是在评论中容易解释的,但我建议您查看我最后链接的文档。您需要向窗口添加一个预加载脚本,该脚本将使用 contextBridge 模块将各种函数暴露给您的页面。这些函数将通过 ipcRenderer 发送消息到主进程,主进程将处理请求。 - pushkin
谢谢@pushkin!10年来一直无法解决,但现在一切都好了!!! - RealZombs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接