【问题标题】:Electron application exits immediately after starting电子应用程序启动后立即退出
【发布时间】: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,并且在我卸载和重新安装节点时没有重置。

标签: node.js electron


【解决方案1】:

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

在升级过程中,某些过程花费了异常长的时间才能完成,但当它完成时,我的应用程序运行成功。我还能够降级到以前的 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"
        }
    },
    [...]
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多