Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allowed to load local resource - Electron

Tags:

electron

When I run my Electron app through Visual Studio Code, the main process loads, which in turn launches the index.html page. In the index.js script I redirect the browser window to a local html file called startup.html, located in my scripts folder, which is just a sub folder of the app. The index.html page does not even launch and the app generates an error with the message:

Not allowed to load local resource

In the DevTools console it also shows the resource that it is attempting to load:

file:///usr/local/lib/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/scripts/ui/startup/startup.html

If I run npm start from my project's root folder, the app launches correctly and both the index.html and startup.html pages get loaded.

Visual Studio Code launches electron with:

/usr/local/bin/electron

This appears to be different than launching it with just npm start. Not sure what the difference is.

A side note: Before I added the code to launch startup.html, the app would run from Visual Studio Code. Only after adding startup.html does it not work.

What could be causing this?

like image 958
Johann Avatar asked Jan 08 '18 09:01

Johann


2 Answers

I have a small electron app that loads the main.js and main.htm from an /app subfolder.

The main.js loads fine and the application creates a window: let mainWindow = null

app.on('ready', () => {
    console.log('Hello from Electron');
    mainWindow = new BrowserWindow();
})

Then I added the code to load the main.htm file that is also located in the /app subfolder.

mainWindow.webContents.loadFile("main.htm")

However, I got the following error in the (chrome) console:

not allowed to load

The error says,

"Not allowed to load local resource:"

That is a red herring. The error should say "cannot find local resource."

If I expand the path I finally see that it is attempting to load the main.htm file from the root directory of my project -- even though the main.js runs from the /app subfolder (which is where the main.htm file is found).

To fix the issue, I simply had to add the subfolder to the path it is was fixed:

mainWindow.webContents.loadFile("app/main.htm")

Most likely this error occurs because your path to the file is incorrect, not because of user rights or whatever.

if you add this line of code you will see the path that it considers the cwd (current working directory:

console.log(`${__dirname}`)
like image 137
raddevus Avatar answered Oct 14 '22 02:10

raddevus


Here is how i solve this in my linux environment and windows environment

newWindow.loadFile(`${__dirname}/index.html`);  //note Linux environment 


newWindow.loadFile(`file://${__dirname}/index.html`) //windows environment

for me my folder structure is

|__app
| |__main.js
| |__renderer.js
| |__index.html
|__package.json

you can also use console.log(__dirname) to view your directory in console/terminal

like image 20
Slycreator Avatar answered Oct 14 '22 03:10

Slycreator