Not allowed to load local resource - Electron - 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?

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:
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}`)

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

Apparently something changed in the updated version of Electron that broke with the VS Code config settings. The documentation on how to configure VS Code has been updated at:
https://electronjs.org/docs/tutorial/debugging-main-process-vscode

So this issue may occur if we trying to manipulate the contents of the page without completely loading the page. So any manipulations must be done after loading the page gracefully(after ready-to-show event).
I also got the same issue i placed the below line before loading the file.
window.webContents.openDevTools()
Example Code
// Issue code
window = new BrowserWindow({width:800,height:600,parent:mainWindow})
window.webContents.openDevTools()
window.loadURL(url.format({
pathname: path.join(__dirname,'/../views/file.html'),
protocol: 'file',
slashes: true
}))
// Issue Solved code
window = new BrowserWindow({width:800,height:600,parent:mainWindow})
window.loadURL(url.format({
pathname: path.join(__dirname,'/../views/file.html'),
protocol: 'file',
slashes: true
}))
window.webContents.openDevTools()

Related

Run Electron main process from URL

I am using webpack-dev-server to build and serve an electron app. I am using the multi-target support so it builds and serves both the Renderer and Main files. These are available at http://localhost:8080/, in my case the Main entry point is http://localhost:8080/background.js, and the Renderer index.html is http://localhost:8080/ which loads http://localhost:8080/js/main.js.
webpack-dev-server only builds the files in memory - they are never written to disk. However Electron only seems to run its Main process from files on disk. I.e. if I run electron path/to/background.js it works fine, but that file doesn't exist when using webpack-dev-server. If I run electron http://localhost:8080/ it just loads the Renderer index.html and never runs background.js.
Basically I want to do
electron http://localhost:8080/background.js
and have it download and run that file as the Main process. That command doesn't work (it just opens the background.js file as text. Is there any way to achieve this short of doing it manually with curl or whatever?
I settled on a workaround - you can make webpack-dev-server serve its output from memory and write the files to disk. I added this to the webpack config for my Main process:
devServer: {
writeToDisk: true
}
Note that due to a bug you need to add it to your Renderer config too, even though you don't need those files, otherwise nothing gets written.

Dart Analyzer in a Rail Project is Giving an Error on a Template File

I'm trying to set up the dart-rails gem. When I created my first dart code file, I got this error:
ERROR: Target of URI does not exist: 'package:rails_ujs/rails_ujs.dart' ([dart_app] dart_app.dart:1)
When I double click on the error, it brings up the following file.
app/vendor/bundle/gems/dart-rails-0.4.3/lib/dart/rails/generators/templates/dart/dart_app.dart
This is the contents of the file:
import 'package:rails_ujs/rails_ujs.dart';
RailsUjs ujsHelper;
void main() {
ujsHelper = new RailsUjs();
}
I have this same file in my app/assets/dart directory as well as the package itself. That file is not showing an error. If I change the code to something invalid, an error pops up. This proves the analyzer is looking at it as well.
The error pops up when I run Reanalyze all Dart source files (without restarting Dart Analysis Service). This is a button in the RubyMine IDE. I tried restarting the service and reanalyzing. I get the same error. This issue seems to be that the analyzer is scanning the entire project for dart files. Is it possible to control where the analyzer looks? It's RubyMine that is running the analyzer, not me at a terminal prompt.
This turned out to be a RubyMine issue. I fixed it by placing a file called .analysis_options in the root of the project with the following contents:
analyzer:
exclude:
- 'vendor/**'
I just excluded everything in the vendor directory. Even when I was getting the error, the analyzer continued to process all the valid code files.
If you import the file using
import 'package:rails_ujs/rails_ujs.dart';
the file rails_ujs.dart has to be in thelibsub-directory of the directory where thepubspec.yaml` file is
he pubspec.yaml file needs a name: rails_ujs property.
pub get needs to be run from the directory that contains the pubspec.yaml file

Packaging Electron Apps with external files

In my Electron application, I have a button in which a user can click which triggers node's child_process and runs an external .jar file in the background:
exec(`java -jar encoder.jar -i filein -o fileout`, function(err, stdout, stderr)
The actual .jar file is located within the root directory of the project. When running in development mode(unpackaged) this works great. However, when I package it and run the application, I get the following error:
There was an error: Error: Unable to access jarfile encoder.jar
Am I to assume that everything in the project folder gets packaged when using electron-packager? If not, what am I missing?
look into process.resourcesPath

javafx native package error invoking method

I'm developing a desktop application using javafx v8.0.60. I have created an exe package with ant in netbeans 8. When I run exe file in my computer, it is installed and run without any problem.
On the other hand, when I try to install and run it on some other computer, at the end of installation, window dialog pops up:
"Error invoking method"
I click Ok. Another window pop up saying:
"Failed to launch jvm"
Davood, greetings! I had this same problem and I, like you, found no help anywhere.
I submit to you a solution, which miraculously worked for me and helped me make sense of those blasted "Error invoking method." and "Failed to launch JVM" dialogs:
Find your .jar file
It has the same name as your Project and it's in your application's installation directory under AppData\Local\{ApplicationTitle}\app (shortcut: type %appdata% into explorer); if your project was named HelloWorld, there you will find HelloWorld.jar
Navigate to it's directory in command prompt
shift+Right Click any blank spot in the Explorer window and choose "Open command window here" (that's a fancy trick I recently learned; alternatively you would cd to the same directory using the command prompt)
Run your .jar via the command line
type java -jar "HelloWorld.jar" and hit Enter
Tadah! Behold your hidden exceptions (the existence of which "Error invoking method." so vaguely tries to communicate to you). *
If your problem is similar to mine it stems from a file structure difference between the project out folder and the installation directory, and that's why the program compiles just fine in the editor and builds just fine—there isn't a problem until it's built out, and the file structure is a little different.
*If you didn't get anything when you ran it via the command line, look for any errors that could be happening during that initialize() method; that's where your problem likely is. You can expose any exceptions during runtime by using a Popup Exception Dialog like shown in a similar problem, here.

node-inspector doesn't let me open a specific js file

When debugging with node-inspector, I can see all of the files in a folder except one.
I checked on the filesystem and the .js is there.
Ctrl+O doesn't let me select the file as well.
How can I open the file to place a breakpoint?
Thanks,
PS: I'm using Node Inspector v0.8.0 and node v0.10.33 on a Win7 32 bits
Node Inspector has two mechanisms for loading files in the GUI.
All files loaded by Node.js (V8) runtime are always listed in the GUI, this works very reliably. The downside is that files not loaded yet are not listed. This typically happens when your application is stared via node-debug or node --debug-brk.
There is a speculative algorithm trying to guess what files are part of your project and included them in the GUI even though they are not loaded in the runtime yet. The algorithm assumes that your project has package.json in the root directory and that the main script file is either in the project root directory (node index.js) or one-level deep (node bin/gpii.js). Additionally, if there is package.json in the current working directory, all javascript files in the current working directory and subdirectories are included too.
See lib/ScriptFileStorage.js for more details.
I suspect that your project is laid out in such way that Node Inspector does not recognise it and thus does not scan it for all javascript files.
You can verify this assumption by running the following code in Node Inspector's Console window while the process is paused, replacing ROOT with the real path to your project root:
require('ROOT/universal/gpii/node_modules/flowManager/src/FlowManager.js')
The file FlowManager.js should appear in Node Inspector after the command has finished.

Resources