user9105857
user9105857

Reputation:

Can not open my BrowserWindow with electron?

I am new to Electron and I followed the documentation to just create my first hello world application using electron, but I have this error :

> [email protected] start C:\Users\user\Desktop\Electron\elc
> electron .


App threw an error during load
Error: Cannot create BrowserWindow before app is ready
at createWindow (C:\Users\user\Desktop\Electron\elc\main.js:10:11)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\main.js:23:16)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\main.js:41:3)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at loadApplicationPackage(C:\Users\user\Desktop\Electron\elc\node_modules\electron\dist\resources\default_app.asar\main.js:287:12)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\node_modules\electron\dist\resources\default_app.asar\main.js:329:5)

And a pop-up telling me the same thing, And I don't know how to solve the problem.

That is my file main.js :

const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require('url')

let win;

function createWindow() {
  win = new BrowserWindow({width:200,height:300});
  win.loadURL(url.format({
    pathname: path.join(__dirname,'index.html'),
    protocol: 'file:',
    slashes: true
  }));
win.webContents.openDevTools();

win.on('closed',()=>{
      win=null;
 })
}

app.on('ready',createWindow())

app.on('window-all-closed',()=>{
if(process.platform !== 'darwin'){
app.quit();
}
})


 app.on('activate', () => {
    if (win === null) {
      createWindow()
      }
    })

And that is my html code :

<!DOCTYPE html>
<html lang="en">
   <head>
     <meta charset="UTF-8">
     <title>Electron Quick Start</title>
   </head>
 <body>
   <h1>Hello World</h1>
 </body>
</html>

And that my file package.json :

{
"name": "tofita",
"version": "1.0.0",
"description": "tofita",
"main": "main.js",
"scripts": {
    "start": "electron ."
},
"author": "Tofita",
"license": "ISC",
  "dependencies": {
    "electron": "^1.8.2"
  }
}

I get the error after executing the ' npm start ' command.

I hope my problem was well explained, any help would be much appreciated.

Upvotes: 1

Views: 3060

Answers (2)

VIGNESH S
VIGNESH S

Reputation: 31

Use this

app.whenReady().then(createWindow);

Upvotes: 0

0.sh
0.sh

Reputation: 2752

you are executing the createWindow function , you have to pass it as a reference not calling the function

app.on("ready", ..... )

do this

app.on("ready", createWindow);

Upvotes: 9

Related Questions