电子应用程序启动后立即退出

时间:2019-12-09 18:30:24

标签: node.js electron

我在Windows 7上运行了Electron 5环境。 电子5已安装在本地node_modules中。 我能够从VSCode开发和运行该应用程序。

我跑了'ncu'并选择升级到Electron 7。 这将电子(从5.0.0升级到7.1.3)和socket.io模块(从2.1.0升级到2.3.0)。 现在,当运行'electron。'时,我的应用程序启动,但在呈现窗口之前退出。 这是因为我将BrowserWindow设置为show:false,并在从未发送过的ready-to-show事件中公开了它。 我曾尝试降级到Electron 5.0.12,但即使删除了node_modules,行为仍然存在。

我有一个Electron 7(7.0.0)的全局实例,但是应用程序在HTML中的每个document.write(process.versions.electron)语句中使用的是版本5。 无论如何,我都将全局电子版本升级到7.1.3以匹配。

然后我复制了电子快速启动文件,并用许多调试语句对其进行了修改。该文件是:

// 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()或类似名称。除了“发生了什么事?”之外,我的明显问题是:如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我没有这个问题的实际答案,但是当我在错误报告https://github.com/electron/electron/issues/21544#issuecomment-577242668中评论时,升级到Electron 7.1.9时,此问题已得到解决。

在升级过程中,某些过程花了很长时间才能完成,但是当我的应用程序成功运行时。我还能够降级到Electron的早期版本,即使使用的是较早失败的版本,我的应用程序仍然可以运行。

编辑:

当由于应用程序在调试器中运行而尝试升级到Electron 8.2.2失败时,此问题再次浮出水面。不幸的是,没有任何版本的升/降级(最高8.2.3)无法恢复环境。最终,从我的package.json启动脚本中禁用了沙箱,使我得以继续。这是新的命令结构:

"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Electron: Main",
        "protocol": "inspector",
        "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
        "runtimeArgs": [
            "--remote-debugging-port=9223",
            "--no-sandbox",              <<<<<-- PASS THIS COMMAND OPTION
            "."
        ],
        "windows": {
            "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
        }
    },
    [...]
]