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
這里也沒有定義。
添加回答
舉報