首页 > 解决方案 > 电子应用程序不会启动,准备就绪时不存在

问题描述

我已经做了几天了,我已经从 Electron GitHub 克隆了电子快速启动(https://github.com/electron/electron-quick-start)我收到了这个错误,但它不是t 只为这个应用程序,它是所有的应用程序。我不知道发生了什么。电子最新版本:8.2.0 错误

app.whenReady().then(createWindow)
     ^

TypeError: Cannot read property 'whenReady' of undefined
    at Object.<anonymous> (/Users/user/Downloads/electron-quick-start-master/main.js:25:5)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

代码:

const electron = require('electron')

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // 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 () {
  // 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 (BrowserWindow.getAllWindows().length === 0) 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.

包JSON

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^8.2.0"
  }
}

标签: javascriptnode.jselectronnode-modules

解决方案


本身不是一个答案——我只是在Electron我目前正在进行的一个项目中尝试了这个,它运行良好:

app.whenReady().then((choice) => {
    console.log("hey, I'm ready", choice);   
})

对于现实检查,我建议使用 ready 事件:

app.on('ready', function () {
  console.log("hey, I'm ready too!");   
});

Electron尽管我突然想问:您使用的是什么版本?如果您使用的是 7 或 8 之前的版本,他们还没有开始“承诺”的东西(我忘记了开始使用的版本Promises


推荐阅读