Javascript Electron.js错误:未定义require

Javascript Electron.js错误:未定义require,javascript,electron,Javascript,Electron,我正在编写我的第一个electron应用程序,我试图在点击按钮时显示一个窗口。我收到一条错误消息: 未捕获引用错误: 没有定义需求 在dialog.js:2 我正在使用“electron nightly”版本“^6.0.0-nightly.20190213” 下面是代码: index.js: const electron = require('electron'); const {app, BrowserWindow} = electron; const path = require('path

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

未捕获引用错误: 没有定义需求 在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);
 <!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>
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();
});
index.html:

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);
 <!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>
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+有关?

您可能必须在新窗口中启用节点集成(根据以下条件默认禁用):

index.js

函数main(){
win=新浏览器窗口({
宽度:500,
身高:400,
网络首选项:{
无融合:对
}
});
win.loadFile(“index.html”)//轻松加载本地html文件
}