I cannot set the page that will be opened by the win.loadfile(') function

Asked

Viewed 46 times

2

Good morning to you all. I’m a beginner with Electron and I can’t open mine index.html through function win.loadFile(). Simply open the following screen: inserir a descrição da imagem aqui Could someone explain to me how to solve this and why it occurs? Dude main.js:

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

let win

function createWindow () {

  win = new BrowserWindow({ width: 800, height: 600 })


  win.loadFile('index.html')  


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

UPDATE: I was able to solve by reinstalling Electron.

  • 1

    The message on the screen says that for you to run the local app you have to run the command below, came to do this?

  • 1

    Yes, I did, but there was an error. I redid the installation of Electron and it worked. Thanks for the reply.

1 answer

1

I usually do it this way:

const electron = require('electron');

const { app, BrowserWindow, Menu } = electron;

let mainWindow;
let commentWindow;

app.on('ready', ()=> {
    mainWindow = new BrowserWindow({});
    mainWindow.loadURL(`file://${__dirname}/index.html`);
});

Note that I used a Node environment variable called __dirname which represents the current directory of the app with the function loadURL which enables you not only to open a local HTML file, but also an external URL.

You mentioned that you reinstalled Electron and it worked, your problem may have been in the package.json file configuration that in the property scripts must contain the following property in the object:

"nameApp": "Electron ."

See example below:

{
  "name": "messagebook",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "nomeApp": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^7.1.10"
  }
}

So above just use the following command line to start your Electron App:

npm rum nomeApp

In the case nameApp you can change to the name you want. Any doubt I remain only update.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.