9

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.

sri vignes
  • 163
  • 1
  • 2
  • 8

2 Answers2

13

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.

ccnokes
  • 6,885
  • 5
  • 47
  • 54
0

You should provide a protocol prefix to the path. For example: "file://[your path here]".

Linh
  • 1,024
  • 2
  • 16
  • 28