2 回答

TA贡献1966条经验 获得超4个赞
从 Electron 5 开始,渲染器进程中的节点集成默认是禁用的。为了解决这个问题,您需要nodeIntegration: true在实例化您的BrowserWindow.
// In the main process.
const { BrowserWindow } = require('electron')
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
编辑:从 Electron 12 开始,您还需要定义contextIsolation: false才能执行此操作,因为标志的默认值已更改。
https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true

TA贡献2016条经验 获得超9个赞
require ('index.js');
在 script 标签中不起作用的原因是require
没有为浏览器定义。它仅针对 Node 定义。你得到ReferenceError
in index.js 的原因是因为<script src="index.js>
实际做的是在浏览器环境中运行 index.js 中的代码。因此,由于它在浏览器中运行,因此require
这里也没有定义。
没有找到匹配的内容?试试慕课网站内搜索吧
添加回答
举报