Electron - Browserwindow does not load pages with the directory other than root

Asked

Viewed 140 times

0

I started to develop an application with Electron, I had never used the framework before, I always used for organization purposes to put the files in separate directories, currently this is my file structure:

IVarejo
|_ package.json
|_ assets
|_ node_modules
|_ src
|  |_ main.js
|_ ui
   |_ index.html
   |_ login.html
   |_ style.css

package json.

{
  "name": "ivarejo",
  "version": "1.0.0",
  "description": "Um software ERP",
  "main": "src/main.js",
  "author": "Samuel Ives",
  "dependencies": {
    "electron": "^7.1.2",
    "mysql": "^2.17.1",
    "node-forge": "^0.9.1",
    "xml-crypto": "^1.4.0"
  },
  "scripts": {
    "start": "electron ."
  },
  "license": "ISC"
}

main.js

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

let win = null;

app.on('ready', ()=>{
  win = new BrowserWindow({width: 600, height: 400});
  win.on('closed', ()=>{
      win = null;
  });

  win.loadFile("../ui/login.html");
});

If I try to load the login.html file that is in the ui folder a blank screen is shown.

But if I move the file to the Ivarejo folder and change the line to the following: win.loadFile("login.html"); will simply work:

inserir a descrição da imagem aqui

So why html files should be in the root directory?

  • And this https://answall.com/a/415240/3635 resolves?

  • @Guilhermenascimento when I pack the program to distribute, it will load the application from within the application?

  • +or-, actually I "think" that it extracts in the TEMP of the operating system (current user), is a control of "Electron"

1 answer

0

Talk buddy! So try this:

win.loadURL(`file://${__dirname}/ui/login.html`);

The variable __dirname is a global Node.js variable that returns the current app directory.

Anything but communicate, my friend!

Browser other questions tagged

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