2

Try to open a HTML file that resides inside the packaged app app.asar file. The file is there but Windows complains that cannot find the file. The pieces of the puzzle are:

  1. inside the win-unpacked\resources\app.asar file there is \dist\doc\index.html
  2. In the main ElectronJS process I confirm the file is here with fs.existsSync("D:\Project\release\1.47.3\win-unpacked\resources\app.asar\dist\doc\index.html")
  3. Then using shell.openPath("<the above path>") to open it but failed with Windows opening a dialog saying cannot find the file and the routine returns "Failed to open path". Well, this makes sense because Windows has no idea of the asar filesystem.

So the central question is: How I open an html file in a browser from the app.asar file?

The temporary solution (I want to avoid for other problems related to the packaging process) is to copy the files under app.asar.unpacked/dist/doc/index.html

5
  • This is the problem: it is known that .asar simulates the real file system via node.js I/O methods. but some of those methods cannot be applied to .asar. I came across one of such cases. Now, shell.openPath is not exactly an I/O API, it is a method of shell, so it doesn't have to work with .asar. If so, it's a shame, of course. Are you sure the packaging without creation of .asar is not an option? If you don't use .asar, everything should work, and the volume of data is usually not so high relative to the other files of the package. Commented Dec 2 at 6:42
  • I also tried the alternative shell.openExternal(fileURL); in my application, it used the file not in .asar (only a user-supplied files), and I would need some to try it out if it is in .asar. You could try it, but I doubt it can make a difference. (To calculate fileURL correctly in a portable way, we also need path.normalize and then url.pathToFileURL.) Commented Dec 2 at 6:58
  • By the way, just in case, I also confirmed on my application that shell.openPath works without .asar and does not work if the path is in .asar. Do you know that packing or not packing resources into .asar is a valid packaging option? I think the solution with not packaging it into .asar can also be used. Your app.asar.unpacked/dist/doc/index.html is also a viable solution. Commented Dec 2 at 7:02
  • I would suggest you copy your index.html file (isn't it the application help file, or something like that?) into the packaged directory by the same script doing packaging, as a post-packaging step... Commented Dec 2 at 7:08
  • 1
    Great and important question, by the way. Commented Dec 2 at 7:09

1 Answer 1

2

Refers to https://www.electronjs.org/docs/latest/tutorial/asar-archives, but for some APIs that rely on passing the real file path to underlying system calls, Electron will extract the needed file into a temporary file and pass the path of the temporary file to the APIs to make them work; If you need to load a file from an ASAR as if it were a normal file on disk, you must copy (extract) it first.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping shell could read asar content. Anyway, I continue putting the help file index.html inside app.asar.unpacked that works very well (except a problem with electron-builder recent versions, but this is another story).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.