Unable to access a file inside asar pacakge
See original GitHub issue- Version: 6.7.3
- Target: Linux, Windows
I have some scripts in my app, which I need to execute when application starts. I am allowing asar packaging in the project. It throws following error at runtime
Can't open /tmp/.mount_ryQosd/usr/bin/resources/app.asar/myfolder/subfolder/myscripts/myscript1.sh
This does not happen during development so this seems to be error with asar packaging, not allowing to access the file.
Although it’s not preferred but I tried unpacking myscripts folder by adding this in build options "asar": { "unpackDir": "myfolder/subfolder/myscripts" }
, it did not work either. I get the same error.
Am I writing the right syntax for unpackDir? Is there any alternative so that script file can be accessed during runtime?
Thanks
Issue Analytics
- State:
- Created 7 years ago
- Comments:33 (11 by maintainers)
Top Results From Across the Web
Cannot access files inside asar archive in electron app
If I try without using app.asar, I was able to access the db file. So Please help me resolve this issue. electron.
Read more >Application Packaging · Electron docs gitbook - imfly
An asar archive is a simple tar-like format that concatenates files into a single file. Electron can read arbitrary files from it without...
Read more >Options | electron-packager
Functions to be called after your app directory has been packaged into an .asar file. Note: afterAsar will only be called if the...
Read more >ASAR Archives - Electron
Most fs APIs can read a file or get a file's information from ASAR archives without unpacking, but for some APIs that rely...
Read more >Common Configuration - electron-builder
in the package.json file of your project using the build key on the top level: ... If you want to use js file,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I understand that it will directly copy the file inside myscripts folder instead of the complete hierarchy. That is as good as just declaring extraResources, I think.
No, it does not work without modification. Because the required file is in different location after packaging so I think we would need to have two different references, one for after packaging and one for without packaging To give you an overview: I have a js file at
app/myfolder/mysubfolder/spawn_script.js
which executes the bash scripts inapp/myfolder/mysubfolder/myscripts
. spawn_script.js is the file where I reference these scripts,at
app/myfolder/mysubfolder/spawn_script.js
filepath = path.join(__dirname, "myscripts/myscript1.sh")
then after packaging filepath==…resourcesDir/app.asar/myfolder/mysubfolder/myscripts/myscript.sh Works without packaging, doesn’t work after packaging(as app.asar is not readable)path.join(process.resourcesPath, "app/myfolder/subfolder/myscripts/myscript1.sh")
then without packaging i.e. in development filepath==…node_modules/electron-prebuilt/dist/resources/myfolder/mysubfolder/myscripts/myscript.sh Doesn’t work without packaging(as file is not there in resources), works after packagingEdit Basically final solution looks like this for me
FWIW I’m also having a painful 1+ day excursion on how to reference a simple file inside the ASAR in the production Node environment. I just wanted to offer some perspective on how I view the problem.
My project is based on https://github.com/chentsulin/electron-react-boilerplate. In my case, I’ve added EJS HTML templates inside my app and want to reference EJS files at runtime to render web pages.
It seems this problem is confusing because there are 3 options:
extraResources
, keep the files outside the ASAR, and use process.resourcesPath to get the path in production. You have to have an if statement do do different stuff between dev and production, just like @makeitcount did above. This is the easiest option, but it’s kind of dirty if you have a lot of static files you’re trying to reference. Plus splitting logic in code everywhere to differentiate between development and production is error prone.asarUnpack
+hazardous
. This seems like the best solution if you need to execute a binary. All I’m trying to do is get a simple reference to a file.NOTE: Debugging this is very time consuming and painful because everything works in fine, but breaks in production. This means you have to continue to rebuild using electron-builder, which takes 4-5 minutes on my project. Each time I want to tweak a simple path or logged path, I have to wait 5 minutes for electron-builder to run to see if it worked. That’s why this is so painful and difficult to debug.
Maybe there is an opportunity for electron-builder or electron (not sure) to improve this API in the future. It seems like a great opportunity for a new API like
process.asarPath
(similar toprocess.resourcesPath
), orapp.getAsarFile()
(similar toapp.getPath()
).One more note: I think the core problem here is that
app.getAppPath()
returns very different answers between development and production. In development, it’s just pointing to my local Electron version, when I want it to point inside my code. In production, it seems correct, but it’s difficult to use to find an actual file inside the ASAR.Thanks for the time you put into working on electron. I’m just trying to offer a perspective. I don’t expect anything to be done specially for me here, just trying to give feedback. 🙏