Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.readFile looks in wrong folder after electron-packager

I'm working on the demo electron-quick-start project. I just added, in the main folder, a test.txt file and this in index.html :

<script>
const fs = require('fs');
alert(fs.readFileSync('test.txt'));        // or ./test.txt or .\test.txt
</script>

It works. But now after having packaged the app with :

electron-packager . --platform=win32 --arch=ia32

when I run the packaged .exe app, it doesn't find the test.txt. Probably because the test.txt is not in the same folder than the .exe but rather in resouces/app/test.txt.

What's the clean way to solve this electron-packager folder issue?

like image 292
Basj Avatar asked Oct 15 '25 07:10

Basj


1 Answers

I solved it by using __dirname :

const path = require('path');
var datafile = path.join(__dirname, 'test.txt');
like image 122
Basj Avatar answered Oct 19 '25 14:10

Basj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!