【发布时间】:2020-04-02 21:34:46
【问题描述】:
我有一个在 Windows 7 上运行的 Electron 5 环境。 Electron 5 安装在本地 node_modules 中。 我能够从 VSCode 开发和运行应用程序。
我运行了“ncu”并选择升级到 Electron 7。
这升级了电子(从 5.0.0 到 7.1.3)和 socket.io 模块(从 2.1.0 到 2.3.0)。
现在,当运行“电子。”时,我的应用程序启动但在窗口呈现之前退出。
这是因为我将 BrowserWindow 设置为 show:false 并将其暴露在 ready-to-show 事件上,该事件从未发送过。
我尝试降级到 Electron 5.0.12,但即使在删除 node_modules 之后,行为仍然存在。
我有一个 Electron 7 (7.0.0) 的全局实例,但应用程序在我的 HTML 中的 document.write(process.versions.electron) 语句中使用版本 5。
无论如何,我将全局 Electron 版本升级到 7.1.3 以匹配。
然后我复制了 electron-quick-start 文件,并用大量调试语句进行了修改。文件是:
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
console.log('READY:create...');
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
console.log('load index...');
mainWindow.loadFile('index-none.html')
// Open the DevTools.
console.log('load devtools...');
mainWindow.webContents.openDevTools()
console.log('set handler...');
// Emitted when the window is closed.
mainWindow.on('closed', function () {
console.log('MAINWIN:Closed...');
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
console.log('created');
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
console.log('onWall-close...');
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
console.log('onActivate...');
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
console.log('setting timer');
setTimeout(()=>{
console.log('timing out...');
}, 15000)
console.log('setting timer2');
setTimeout(()=>{
console.log('timing2 out...');
}, 200)
console.log('setting timer5');
setTimeout(()=>{
console.log('timing5 out...');
}, 500)
console.log('setting timer7');
setTimeout(()=>{
console.log('timing7 out...');
}, 700)
setTimeout(()=>{
console.log('timing10 out...');
}, 1000)
setTimeout(()=>{
console.log('timing12 out...');
}, 1200)
setTimeout(()=>{
console.log('timing15 out...');
}, 1500)
setTimeout(()=>{
console.log('timing17 out...');
}, 1700)
HTML 文件是:
<html>
<head><title>title</title></head>
<body>
Hello world.
</body>
</html>
当我运行“electron eqs.js”或“.\node_modules.bin\electron.cmd eqs.js”时,我得到以下输出:
setting timer
setting timer2
setting timer5
setting timer7
READY:create...
load index...
load devtools...
set handler...
created
timing2 out...
timing5 out...
timing7 out...
timing10 out...
timing12 out...
timing15 out...
(有时timing15 不会被打印出来)。
在这种情况下,我看到 BrowserWindow 被创建,但它仍然是空白的。开发者工具打不开。注释掉打开开发者工具的行没有任何改变。
这两种情况都会发生: C:\electron\electron-quick-start>.\node_modules.bin\electron.cmd -v
v5.0.12
C:\electron\electron-quick-start>electron -v
v7.1.3
我不明白为什么进程在计时器处于活动状态时退出,所以某处必须调用app.exit() 或类似的东西。除了“发生了什么事?”之外,我的明显问题是:我该如何解决这个问题?
【问题讨论】:
-
听起来类似于突然出现的my current issue,,尽管在我的情况下电子窗口甚至从未打开......
-
@oldboy,感谢您的反馈,但我认为这是一个非常不同的问题。我在这里打开了一个错误报告:github.com/electron/electron/issues/21544 -- 目前还有一个额外的外部确认,其中 Windows 10 可以工作,但 Windows 7 不能工作。
-
我想通了。不知何故,npm
ignore-scripts配置键的值更改为true,并且在我卸载和重新安装节点时没有重置。