fs.readFile在electron-packager之后看错了文件夹

Bas*_*asj 3 node.js electron

我正在研究电子快速启动项目.我刚刚在主文件夹中添加了一个test.txt文件,其中包含index.html:

<script>
const fs = require('fs');
alert(fs.readFileSync('test.txt'));        // or ./test.txt or .\test.txt
</script>
Run Code Online (Sandbox Code Playgroud)

有用.但现在打包应用程序后:

electron-packager . --platform=win32 --arch=ia32
Run Code Online (Sandbox Code Playgroud)

当我运行打包的.exe应用程序时,它找不到test.txt.可能是因为它与.exe test.txt 不在同一文件夹中而是在.exe中resouces/app/test.txt.

什么是解决这个电子打包文件夹问题的干净方法?

Bas*_*asj 7

我用以下方法解决了它__dirname:

const path = require('path');
var datafile = path.join(__dirname, 'test.txt');
Run Code Online (Sandbox Code Playgroud)