4

i have a problem with electron.

TypeError: Cannot read property 'whenReady' of undefined

i use node 14.0.1 electron 10.1.2

i run my app "electron:serve": "vue-cli-service electron:serve",

my background.js

const { app, BrowserWindow } = require('electron')
const { server } = require('feature-server-core')

server.start();

function createWindow () {
    // Создаем окно браузера.

    const win = new BrowserWindow({
        width: 1400,
        height: 900,
        minWidth: 1280,
        minHeight: 800,
        closable: true,
        center: true,
        type: "tool",
        titleBarStyle: "hidden",
    })

    win.menuBarVisible = false;
// и загружаем index.html в приложении.
    win.loadURL("google.com")
}

app.whenReady().then(() => {
    createWindow()

    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()
    })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
})
Olian04
  • 6,480
  • 2
  • 27
  • 54
Ruslav Ivanov
  • 41
  • 1
  • 4

5 Answers5

5

This is the problem related to directly calling the 'index.js' with node like node index.js. You need to use electron index.js or electron .(if your file is index.js) electron is building your app.

sharun k k
  • 439
  • 8
  • 16
4

i solved the same problem just typing in the folder's terminal

npm start
Dharman
  • 30,962
  • 25
  • 85
  • 135
DePaS
  • 59
  • 4
1

You can use npm start instead of node . to execute your application. If you are using an IDE you have to configure npm with the start option.

gopeca
  • 1,601
  • 12
  • 12
1
  1. check the js file is named the same in the package.json
  2. check the script "start": "electron ." is in scripts.
  3. npm i then npm start

that should help.

Mohamed Jawad
  • 71
  • 1
  • 5
0

I had the same symptom but the underlying problem was completely different from what you find elsewhere on the Internet. Normally, this issue comes from accidentally running main.js with node instead of electron. But in my case the problem was that the NTFS case sensitivity flag was set on the project folder and some subfolders because they were initially created using WSL. This lead to weird things like electron.cmd not being found due to Windows looking for electron.CMD with upper-case extension, etc., and apparently also some similar problem inside Electron itself which made it think we are not inside Electron after all, giving us the non-Electron version of electron which has no app.

The solution was to create a copy of the whole project folder using Windows Explorer, and using that one instead of the original folder. Windows Explorer doesn't preserve the case sensitivity flag, thus creating a copy of the folder clears the flag in all subfolders too. (You can then just delete the old folder.)

More information about case sensitivity in NTFS: https://www.howtogeek.com/354220/how-to-enable-case-sensitive-folders-on-windows-10/


Another cause could be having the environment variable ELECTRON_RUN_AS_NODE=1 set, maybe from some experiment or test a long time ago. Make sure this variable isn't set.

CherryDT
  • 25,571
  • 5
  • 49
  • 74