Electron.js错误:未定义require

时间:2019-03-10 20:46:41

标签: javascript electron

我正在编写我的第一个电子应用程序,并且我试图在单击按钮时显示一个窗口。我收到一条错误消息:

  

未捕获的ReferenceError:   需求未定义       在dialog.js:2

我正在使用版本“ electron-nightly”:“ ^ 6.0.0-nightly.20190213”

代码如下:

index.js:

const electron = require('electron');
const {app, BrowserWindow} = electron;
const path = require('path');
const url = require('url');

let win

function main(){

    win = new BrowserWindow({ width: 500, height: 400});

    win.loadURL(url.format( {
        pathname: path.join(__dirname, './index.html'),
        protocol: 'file',
        slashes: true
    } ));

    win.webContents.openDevTools()

}

exports.openDialog = () => {
    let dial = new BrowserWindow({ width: 400, height: 200});

    dial.loadURL(url.format( {
        pathname: path.join(__dirname, './dialog.html'),
        protocol: 'file',
        slashes: true
    } ));
}


app.on('ready', main);

index.html:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello world</title>
</head>
<body>

    <h1>Hello Electron!!</h1>

    <button id="btn">Show dialog</button>

    <script src="./dialog.js"></script>

</body>
</html>

dialog.js:

const index = require('electron').remote.require('./index.js'); //Error line: Uncaught ReferenceError: require is not defined at dialog.js:2

const button = document.getElementById('btn');

button.addEventListener('click', () => {
    index.openDialog();
});

此错误与ES6 +有关吗?

1 个答案:

答案 0 :(得分:0)

您可能需要在新窗口中启用节点集成(默认情况下根据documentation禁用):

index.js

ruf=