首页 > 解决方案 > 电子应用程序中未定义require

问题描述

我是电子新手。在学习它时,我require not define在浏览器窗口上遇到了错误。所以我搜索了一下,发现我们应该添加 nodeIntegration: true,,所以我做到了,但它仍然没有解决我的问题。

这是我的 app.js(入口文件)

function createWindow() {
  win = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
    },
  });
  win.loadURL(
    url.format({
      pathname: path.join(__dirname, "index.html"),
      protocol: "file",
      slashes: true,
    })
  );

  win.on("close", () => {
    win = null;
  });
}

app.on("ready", createWindow);

和我的 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>main renderer</title>
    <link rel="stylesheet" href="./styles.css">
</head>
<body>
    <h1>main renderer</h1>
    <script>
        require("./index.js")
    </script>
</body>
</html>

这是错误图像

标签: javascriptelectron

解决方案


推荐阅读