Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access files inside asar archive in electron app

Tags:

electron

Electron version: 1.2 Operating system: Windows

Cannot able to access the files inside asar archive using readFileSync:

var fs = require('fs');
var SQL = require('sql.js');
var filebuffer = fs.readFileSync('asar:'+ __dirname + './app/data/sample.db');

I've also tried using 
trial 1 : readFileSync('D:/Sample-App/app.asar/app/data/sample.db');

trial 2 : readFileSync('./app/data/sample.db');

trial 3 : process.noAsar= true;
readFileSync('./app/data/sample.db');

None of the trials worked out. If I try without using app.asar, I was able to access the db file. So Please help me resolve this issue.

like image 667
sri vignes Avatar asked Jun 01 '16 04:06

sri vignes


1 Answers

I'm assuming you mean that once you package your app to an asar, your file paths don't work.

fs.readFileSync('asar:'+ __dirname + './app/data/sample.db')

This is close. You don't need "asar:" and you should use the path module to make your life easier.

Try constructing a path dynamically using path. Also your main entry point JS is probably already running in the app folder. Try something like this:

path.join(__dirname, '/data/sample.db');

This will create a valid, absolute path for each operating system (paths in Windows use \ whereas OSX and Linux uses /). Remember __dirname is whatever your current directory is, so everything after that has to be relative to it.

like image 106
ccnokes Avatar answered Oct 19 '22 18:10

ccnokes